Functions and Subroutines - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Functions and Subroutines

Description:

A general procedure can be used with different arguments as long as the ... The product of a sub procedure depends on the ingredients (arguments) passed to it ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 29
Provided by: haimin8
Category:

less

Transcript and Presenter's Notes

Title: Functions and Subroutines


1
Functions and Subroutines
  • 3/10/2003

2
More about Arrays
  • Key word Redim
  • Dim SomeArray() as SomeType
  • intSomeNumber 28 (some value)
  • Redim SomeArray(intSomeNumber) as SomeType
  • Keep track of array size
  • Declare array size at the form level
  • Keep track of it all the time when you add a
    new element, increase it by one when you delete
    an element, decrease it by one.

3
Using General Procedures
  • Event procedures are associated with a particular
    event and are not usually available to other
    forms.
  • General procedures are used for specific tasks
    that are not associated with an event.
  • General procedures are defined in the General
    Object of a form and then invoked elsewhere in
    the project.
  • Write it once and make it available at every spot
  • Two types of general procedures
  • subs (subroutines)
  • functions

4
Why General Procedures ?
  • Divide and Conquer break complexity into parts
  • Keep a general purpose task done in one piece of
    code, reuse it every time its needed

5
Built-in Programmer-defined
  • Weve used Provided procedures
  • Functions IsNumeric(), Eof(), Date
  • Subs like Msgbox, Format( string, format)
  • You can write functions too
  • 1. Break complexity into pieces
  • 2. Avoid repeating stuff
  • 3. Parameterize re-useable code

6
Creating a Function
  • The general form of the function definition
    statement is
  • Function intFindMax(intNum1 as Integer, intNum2
    as Integer) as Integer
  • logic
  • End Function
  • The function definition defines f(x), type of
    output, type and number of inputs
  • An important rule is that the name of the
    function must be assigned a value in the function
    definition this value will be returned to the
    calling procedure (assigned to y)
  • Mathematical function y f(x) three pieces
  • The function definition in terms of x
  • E.g. f(x) pi x2
  • F(x) 2 pi x
  • The input x
  • Assign the function value to the output y

7
Subs and Functions
  • A sub is a unit of code that performs a specific
    task but returns no value.
  • A function is similar to the built in functions
    in that arguments are passed to it and processed
    to compute a single value returned by its name.

8
Design a general procedure
  • Decide
  • what the we want it to do
  • What input and desired output are
  • What logic is required to convert the input into
    the desired output
  • Input is usually through the arguments
  • Output for a function is usually through its
    name, and output from a sub is usually through
    the arguments

9
Creating a Sub
  • The general form of the sub definition statement
    is
  • Sub SubName (parameter1 as type, parameter2 as
    type, )
  • End Sub
  • Note that the sub name is not assigned a data
    type, because it is not the sub itself what is
    going to be returned, the return is through the
    argument(s)
  • Example
  • Sub Switch(curFirst as Currency,curSecond as
    Currency)
  • lotic
  • End Sub

10
Creating Subs and Functions
  • To create a sub or function you can
  • Use the ToolsAdd Procedure menu command and
    select the type of procedure to add
  • or
  • simply type the word Sub or Function and press
    Enter after any existing event or general
    procedure.
  • In either case, then add the parameters in
    parentheses.

11
Invoking a General Procedure
  • General procedures must be created and then
    invoked.
  • Arguments in the definition and parameters passed
    to the procedure are nothing but variable names.
  • Invoking a function assign the function to a
    variable
  • variable functionname(arg1, arg2, , argn)
  • Invoking a sub
  • subname arg1, arg2, , argn
  • Or
  • Call SubName (arg1, arg2, , argn)

12
Using a General Procedure
  • The name of the arguments and parameters need not
    be the same its the order that is important
  • A general procedure can be used with different
    arguments as long as the arguments match the
    parameters
  • The product of a sub procedure depends on the
    ingredients (arguments) passed to it

13
Example - Sub Procedure
  • Sub MixBake (ingredient 1, ingredient 2,
    ingredient 3)
  • 1. Mix ing1 2
  • 2. Stir the mixture of step 1 for two minutes
  • 3. Put mixture in ing3 and bake for 15 minutes
  • 4. Put product in ing3
  • End Sub

14
Using the Procedure
  • What I get also depends on the order of the
    arguments I pass
  • Call MixBake baking pan, flour, milk
  • Baked mixture of flour and baking pan is returned
    in milk!
  • That is NOT what I want!
  • Too bad. But that is what you asked the computer
    to do.
  • What I get depends on what I pass to the
    procedure
  • Call MixBake flour, milk, baking pan
  • A cake is returned in the baking pan
  • Call MixBake chemical 1, chemical 2, pan
  • A new chemical is returned in the pan

15
Relationship Between Sub Definition Statement and
Statement Invoking the Sub
16
Relationship Between Function Definition
Statement and the Statement Invoking the Function
17
Matching Arguments and Parameters
  • For both sub and functions, the number, type, and
    order of arguments in the invoking statement must
    match the number, type, and order of parameters
    in the procedure definition statement. Note that
    the names of the variables do NOT need to match.

18
Array Arguments
  • In both the argument and parameter list,
    fixed-size arrays are referenced by the name of
    the array followed by parentheses.
  • Pass array size as an argument to the procedure
  • Sub definition
  • Sub Sort(intNumber as Integer, strList() as
    String)
  • Calling statement
  • Sort intNumCustomers, strCustomers()

19
Global Declarations and the Code Module
  • A Code Module is a section of pure code that is
    known to all parts of the project.
  • Use Public statement to declare variables
  • Public varName1 as type, varName2 as type, ...
  • When variables are declared in a code module
    using key word public, they are known to the
    entire project in contrast to form-level or
    procedure level variables that are known only to
    the form or to the procedure

20
Scope of Global Variables
21
Assignment D
  • It is hard lots of work new material much of
    it from chapter 7 none from chapter 8
  • Do you wait until you know enough?
  • Doesnt seem like a good idea
  • How do you start before you know enough?
  • ?by doing things you already know how to do
  • What are they?

Borrowed from Dr. Madeo
22
What are They?
  • Putting things on a form
  • Deciding which events require implementation
  • This must includes click events for each command
    button
  • Of course, you can see the command buttons when
    you execute the .exe file
  • You may know what it to happen during each of them

Borrowed from Dr. Madeo
23
  • If you arent ready to write all the code, write
    what you can and place comments describing whats
    to be done
  • To the extent possible, RUN what you have. Itll
    get rid of syntax errors and maybe some simple
    logic errors

Borrowed from Dr. Madeo
24
  • In particular, you ought to test out the process
    of going from form to form and make sure it
    works.
  • You should put your name on each form
  • You should put every part of what you are doing
    in a folder whether it be on a floppy, a zip
    disk, your N drive,
  • I often start a project this way

Borrowed from Dr. Madeo
25
  • Getting an early start is particularly important
    if you have decided not to be in class on one or
    both of Fri. 3/14 and Mon. 3/24
  • We will definitely post a solution prior to Exam
    Three on April 2 it is not as clear that the
    assignments will be graded before the exam

Borrowed from Dr. Madeo
26
Executive Summary
  • Its due on Monday, March 31
  • The good news was that we moved back the due date
    on Assignment D giving you more time
  • The bad news is that you dont have lots of time
    AFTER Assignment D to do the Executive Summary

Borrowed from Dr. Madeo
27
  • We are serious about two elements of the
    executive Summary
  • 1. The time it is due. Dont be late.
  • 2. The length. The body of the paper is not to
    exceed two pages.
  • The body of the paper does not include
  • The optional cover page with your name
  • The non-optional list of 5 or more references no
    more than 40 of which can be purely from the
    web. You might se the sources of full text
    online availability of articles through the
    techy stuff in the library
  • Of course, it is expected that your summary will
    exceed 1.5 pages

Borrowed from Dr. Madeo
28
Final Exam Time
  • Although it is possible that there will be a
    last minute denial, we have been told to act as
    if our request for having a common 4600 exam on
    Tuesday, May 6 from 7 to 10 p.m. has been
    approved.
  • If you have a conflict, please see me as soon as
    possible

Borrowed from Dr. Madeo
Write a Comment
User Comments (0)
About PowerShow.com