Title: Chapter 5 Writing Procedures and Functions
1Chapter 5Writing Procedures and Functions
5
CI 9008 George Zolla
IS 2020
Prof
. Gary Porter
grporter_at_
nps
.navy.mil
Naval Postgraduate School
Monterey, CA
2Modular Programming Advantages
- Modular programming has several advantages.
- 1. It allows the programmer to logically organize
a program by dividing the problem into smaller
sub-problems. - Repeatedly using this divide and conquer
technique to solve a problem is called stepwise
refinement and is part of the top-down design
programming methodology - For instance, a programmer may write a program
using three subprograms one subprogram contains
the input routine, another performs calculations,
and a final subprogram displays the output
3Modular Programming Advantages
- 2. Modular programming enhances the readability
of the code and is easier for the programmer to
debug. - 3. Like the components in a stereo system, a
module can be replaced (or upgraded) to improve
the program - 4. Modular programming allows for the reuse of
code.
4Modular Programming Advantages
- Code reuse is an important factor in computer
programming. - Once a programmer designs a subprogram to perform
a certain task in one program, it is not
desirable to recreate the subprogram to perform
the same task in a different program. - A better option would be to simply reuse the
existing code. - This re-emphasizes the importance of adequate
documentation. - The documentation must specify the required
module inputs and the type of output.
5Subprograms
- Subprograms
- Procedures and functions comprise the two types
of subprograms. - You have already seen examples of both in VB.
6Arguments and Parameters
- Do you notice any similarity between procedures
and functions program? - In the procedure declaration the Click event has
parentheses following it - it is written as Click().
- Similarly, all of the built-in functions have
parentheses that contain items following their
names. - Parentheses enclose the information passed to
subprograms.
7A Sub Procedure Called Adder
- Private Sub Adder(num1 As Double,num2 As Double,
_ - sum As Double)
- 'Add num1 and num2 and store the result in sum
- sum num1 num2
- End Sub
- num1, num2, and sum are the parameters of the
Adder procedure. - Parameters are merely placeholders for the
information passed to the procedure when it is
invoked. - The parameter list specified in the Adder
procedure contains three elements, thereby
informing the VB compiler that the Adder
procedure requires three double-precision numbers
to be passed to it.
8Subprogram Arguments and Parameters
- The number of arguments must equal the number of
parameters. - )Order is important. The first argument
corresponds to the first parameter, the second
argument to the second parameter, and so on. - )The data type of each argument must match the
data type of its corresponding parameter. - )Names are not important. The name of an
argument does not have to correspond to the name
of its parameter. - )Recognize the manner in which data is passed,
either by reference or by value. These topics
are discussed in a later section.
9Defining and Using Sub Programs
- Procedures and functions that are not built-in to
VB are called user-defined because the programmer
(the user of the compiler) must define them. As
shown in the example in the previous section, the
general form for defining a procedure is - Private Sub ProcedureName (parameter1 As Type1,
parameter2 As Type2, ...) - statements
- End Sub
- A procedure is invoked using the Call statement.
The syntax of the Call statement follows - Call ProcedureName(argument1,argument2, ...)
10The Call keyword
- The Call keyword is optional, but the syntax of a
VB procedure call changes when the Call keyword
is not used. The following syntax performs the
same procedure call as the Call statement above - ProcedureName argument1, argument2, ...
- In this syntax, notice the absence of parentheses
surrounding the arguments. - A common mistake is to use parentheses, and this
can lead to syntax or logical errors as described
in the next section. - Additionally, this syntax makes it more difficult
for a programmer to distinguish between a
procedure call and a function call.
11Functions
- Functions are somewhat different from procedures.
- A function may take any number of arguments, but
it always returns a single value to the calling
routine. - A procedure, on the other hand, does not
necessarily return a value or may return multiple
values. - The general rule is to use a function if you need
to return exactly one value to the calling
routine. - So, our Adder procedure is better written as a
function - Private Function Adder(num1 As Double, num2 As _
Double) As Double - 'Add num1 and num2 and return the result
-
- Adder num1 num2
- End Function
12General Form of a User-defined Function
- Private Function FunctionName(param1 as Type1,. .
.) As FunctionType - statements
- FunctionName ReturnValue
- End Function
- Notice that the function is defined with a
specific data type (FunctionType) - this is the data type of the value that the
function returns to the calling routine. - In order for the function to return a value to
the calling routine, the value must be assigned
to the function name as shown in the line
preceding the End Function statement.
13Function Invocation
- Unlike a procedure invocation, a function
invocation cannot use the Call term as part of a
function call. - A function is invoked simply by writing the
function name along with the required arguments.
- Remember that a function returns a single value,
and your program should do something with this
value (store it in a variable, print it, etc.). - The general form of a function invocation where
the returned value is assigned to a variable
follows - variableName FunctionName(arg1,arg2,...)
14Parameter Passing by Reference
- Parameters can be passed either by reference or
by value. - Passing a parameter by reference actually passes
the memory location (address) of the argument to
the subprogram instead of the arguments value. - This allows the subprogram to access the actual
variable. As a result, the variables value can
be changed by the subprogram. - Passing by reference is the default method of
parameter passing in VB i.e., unless otherwise
specified, VB passes parameters by reference.
15Parameter Passing by Value
- Passing a parameter by value, on the other hand,
passes the value of the argument to the
subprogram. - This allows the subprogram to access a copy of
the variable, and the variable's actual value
cannot be changed by the subprogram to which it
is passed.
16ByVal and ByRef Keywords
- The ByVal keyword must precede each parameter
that is passed by value. - Since passing parameters by reference is the
default method in VB, the ByRef keyword is
optional. - VB also provides a way to force arguments to be
passed by value even when their associated
parameters are by reference in the subprogram
heading. - Enclosing an argument in parentheses forces VB to
pass the argument by value. - Page 183
17Function Example (186)
- Private Function curPayment(curRate as Currency,
curTime as Currency, curAmt as Currency) as
Currency - curPayment curAmt (1 curRate/12)
(curTime 12) - End Function
- --------------------------------------------------
-------------- - lblPayment.Caption curPayment(curRate, curTime,
curAmount)
18Parameter passing with different parameter names
is OK and helps protect scope of variables
19Too few arguments in procedure call
20?
Argument type mismatch error
21Local variable conflicting with parameter