chapter 4
DESCRIPTION
Chapter 4. General Procedures. Outline & Objective. Creating Visual Basic Sub Procedures Creating User-defined Function Procedures Parameter Passing Mechanism Modularizing in Programming Languages. What is Modularization. Breaking the program into subtasks - PowerPoint PPT PresentationTRANSCRIPT
Chapter 4 - Visual Basic Schneider 1
Chapter 4
General Procedures
Chapter 4 - Visual Basic Schneider 2
Outline & Objective
Creating Visual Basic Sub Procedures Creating User-defined Function Procedures Parameter Passing Mechanism Modularizing in Programming Languages
Chapter 4 - Visual Basic Schneider 3
What is Modularization
Breaking the program into subtasks A Sub procedure or Function performs a
well-defined task Easier to test, debug and correct
Chapter 4 - Visual Basic Schneider 4
Why use Sub procedures and Functions?
Provide abstract operations Make programs easier to write, debug and
maintain.
Chapter 4 - Visual Basic Schneider 5
Modularizing Programs in Visual Basic
In Visual Basic, there are two types of procedures: – Sub– Function
Note: To distinguish procedures from event procedures, Sub and Functions are referred to as general procedures.
Chapter 4 - Visual Basic Schneider 6
Passing Arguments to Subs:
When you define a Sub procedure; sometimes you need to transfer variables that are used in different Subs. This is called passing in programming languages.
Chapter 4 - Visual Basic Schneider 7
Sub Procedures Properties:
may be called may be passed data called arguments may return values to the calling program may change the data stored in a received
variable
Chapter 4 - Visual Basic Schneider 8
Components of Sub Procedure:
name: used to identify the Sub procedure parameters: a Sub procedure accepts values
from the caller through its parameters; it may also send values back to the caller through it’s parameters.
Chapter 4 - Visual Basic Schneider 9
Sub Procedure's Name
The rules for naming Sub Procedures are the same as naming variables.
In this text, Sub procedure names begin with uppercase letters in order to distinguish them from variable names.
Chapter 4 - Visual Basic Schneider 10
Syntax of a Sub Procedure
Private Sub ProcedureName ( )
statement(s)
End Sub
Chapter 4 - Visual Basic Schneider 11
Creating Visual Basic Sub Procedure:
Activate a code window Select Add Procedure from the Tools menu Type in the name of the Sub procedure Click on Private in Scope frame Press the Enter key or click the OK button Type the statements of the Sub procedure
into this window
Chapter 4 - Visual Basic Schneider 12
Example of Call to a Sub Procedure:
Private Sub cmdCompute_Click()
Dim num As Single
num = Val(InputBox("Enter a number:"))
Call Triple(num)
End Sub
Chapter 4 - Visual Basic Schneider 13
Sub Procedure Triple:
Private Sub Triple(num As Single)
' Multiply the value of the number by 3
picResult.Print "The number is"; 3 * num
End Sub
Chapter 4 - Visual Basic Schneider 14
Passing Arguments to Sub Procedures
Arguments : Variables or expressions placed in parentheses in a Call statement.
Not only is the value of the argument passed to the parameter, but the value of the parameter is passed back to the argument.
Chapter 4 - Visual Basic Schneider 15
Example of Arguments
Call Triple(num)
Chapter 4 - Visual Basic Schneider 16
Parameters
Variables placed in parentheses after a Sub Procedure's name.
When the procedure is called, the values of the corresponding arguments are placed in the parameters.
Chapter 4 - Visual Basic Schneider 17
Example of Parameters
Private Sub Triple(num As Single)
Chapter 4 - Visual Basic Schneider 18
Passing arguments to parameters
Call Triple(num )
Private Sub Triple (num As Single)
Argument
Parameter
Chapter 4 - Visual Basic Schneider 19
Passing Arguments to Parameters
Call Add (x, y )
Private Sub Add ( num1 As Single, num2 As Single)
Parameters
Arguments
Chapter 4 - Visual Basic Schneider 20
Passing Arguments
The Sub Procedure receives the location of the arguments, the Sub Procedure may use and modify the value of the arguments.
Two way into and out of the Sub Procedure.
Chapter 4 - Visual Basic Schneider 21
Passing Arguments
Private Sub cmdDisplay_Click()
Dim amt As Single
amt = 2
picResults.Print amt;
Call Triple(amt)
picResults.Print amt
End Sub
Chapter 4 - Visual Basic Schneider 22
Sub Triple
Private Sub Triple(num As Single)
' Triple a number
picResults.Print num;
num = 3 * num
picResults.Print num;
End Sub
Chapter 4 - Visual Basic Schneider 23
Passing Arguments
Call Triple(amt)
Private Sub Triple (num As Single)
amt
num
Chapter 4 - Visual Basic Schneider 24
Passing Data - by Reference
2 2 6 6
amt amt amt
num num
Chapter 4 - Visual Basic Schneider 25
Important Rules for Passing Arguments to a Sub
The number of arguments and parameters must match.
The data type of each argument must match its corresponding parameter.
Chapter 4 - Visual Basic Schneider 26
Local Variables:
A variable that is used only in a specific procedure (Sub or Function).
The scope of the local variable is the portion of a Sub or Function in which that variable has been defined.
Chapter 4 - Visual Basic Schneider 27
Local Variables:
Declared within a procedure definition Private to a procedure definition Variables in different procedures are totally independent different procedures can have variables with the same
names; however, each variable will have its own memory location
Chapter 4 - Visual Basic Schneider 28
Advantages of Local Variables
Extremely useful for team programming To protect side effect (which is an
accidental change of the value of the variable)
Chapter 4 - Visual Basic Schneider 29
Example of Local Variables
Private Sub cmdButton_Click()
Dim var1 As Integer, var2 As Integer,num As Integer
var1 = 2
var2 = 4
Call Add(num)
picBox.Print num
End Sub
Chapter 4 - Visual Basic Schneider 30
Sub Add
Private Sub Add(num As Integer)
Dim var1 As Integer, var2 As Integer
num = var1 + var2
End Sub
Chapter 4 - Visual Basic Schneider 31
Form-Level Variables
Form-level variables are visible to every procedure (Global variable).
Form-level variables appear at the top of the code window.
Chapter 4 - Visual Basic Schneider 32
How to create Form-Level Variables?
Activate the code window Click on the down arrow to the right of the object
list box Click on General Click on Declaration in the procedure list box Type in Dim statements for form-level variables
Chapter 4 - Visual Basic Schneider 33
Example
' In Declaration section of General
Dim num1 As Single, num2 As Single
Chapter 4 - Visual Basic Schneider 34
Review
Visual Basic has two types of procedures:– Sub– Function
Each Sub procedure performs a distinct task.
The Call statement causes a Sub procedure to be executed.
Chapter 4 - Visual Basic Schneider 35
Review
A Sub procedure can receive many values
A Sub procedure can return many values
Chapter 4 - Visual Basic Schneider 36
Review
Values can be passed between the calling program and Sub by passing arguments.
The number and type of arguments in the calling program and Sub must match.
Chapter 4 - Visual Basic Schneider 37
Review
Variables that are used in a particular Sub are local variables.
Values that are assigned to them are not returned to the calling module.
Chapter 4 - Visual Basic Schneider 38
Review
Structure charts are useful in determining how to divide a program into Sub procedures.
Modularizing programs offers significant advantages.– Easier to maintain
– Extremely useful for team programming
– Side effect can be prevented
Chapter 4 - Visual Basic Schneider 39
Common Errors
Passing incorrect data types. Not returning the result of the computation
back to the calling program.
Chapter 4 - Visual Basic Schneider 40
Review
A sub is like a black box; you know what goes in and what comes out; but you do not have to know what happens inside.
To specify the result of the computation inside the box ; the result can only be returned through its arguments.
Chapter 4 - Visual Basic Schneider 41
Another Example
Private Sub cmdDisplay_Click()
' Demonstrate that variables in a Sub procedure do
' not retain their values in subsequent calls
Call Three
Call Three
End Sub
Chapter 4 - Visual Basic Schneider 42
Sub Three
Private Sub Three()
Dim num As Single
' Display the value of num and assign it the value 3
picResults.Print num;
num = 3
End Sub
Chapter 4 - Visual Basic Schneider 43
What is a function?
A function designed to perform a specific task also.
A function designed to return a single value to the calling program.
Chapter 4 - Visual Basic Schneider 44
Types of Functions
Standard functions (built-in) User-defined functions
Chapter 4 - Visual Basic Schneider 45
User-Defined Function
A function designed to return a single value.
The value is returned in the function itself. The arguments of a function should not be
changed in the function body.
Chapter 4 - Visual Basic Schneider 46
The Function Syntax
Private Function FunctionName (parameter-list) As datatype
Statement(s)……
…..
FunctionName = ……..
End Function
Chapter 4 - Visual Basic Schneider 47
Example of a Function(using a function to change from Fahrenheit to Celsius)
Private Sub cmdConvert_Click()
picTempC.Cls
picTempC.Print FtoC(Val(txtTempF.Text))
End Sub
Private Function FtoC(t As Single) As Single
‘ Convert Fahrenheit temperature to Celsius
FtoC = (5 / 9) * (t - 32)
End Function
Chapter 4 - Visual Basic Schneider 48
Rule for Defining and Calling a Function
User-defined function must include a statement that assigns the function name a value.
User-defined functions are called in the same way that built-in functions are called.
A user-defined function may be called in an expression.
Chapter 4 - Visual Basic Schneider 49
Returning Value
A function can receive many values
Only one value can be directly returned
Chapter 4 - Visual Basic Schneider 50
Example of a Function
Private Sub cmdDetermine_Click()
Dim nom As String
' Determine a person's first name
nom = txtFullName.Text
picFirstName.Cls
picFirstName.Print "The first name is "; FirstName(nom)
End Sub
Chapter 4 - Visual Basic Schneider 51
Function FirstName
Private Function FirstName(nom As String) As String
Dim firstSpace As Integer
' Extract the first name from a full name
firstSpace = InStr(nom, " ")
FirstName = Left(nom, firstSpace - 1)
End Function
Chapter 4 - Visual Basic Schneider 52
Common Errors
Passing incorrect data types Not specifying the data type of the returned
value Forgetting the data type of a function's
parameter
Chapter 4 - Visual Basic Schneider 53
Common Errors
Not assigning a value to the function name inside the function definition
Misspelling of the Function name Wrong invoking of the function in an
expression
Chapter 4 - Visual Basic Schneider 54
Further Examples
Private Sub cmdDisplay_Click ()
' Compute Volume of a Cylinder
Dim r As Single, h As Single
r =1
h = 2
Call DisplayVolume (r, h)
r =3
h = 4
Call DisplayVolume (r, h)
End Sub
Chapter 4 - Visual Basic Schneider 55
Function Area
Private Function Area (r As Single) As Single
' Compute area of a circle of radius r
Area = 3.14159 * r ^ 2
End Function
Chapter 4 - Visual Basic Schneider 56
Sub DisplayVolume
Private Sub DisplayVolume ( r As Single, h As Single)
PicOutput.Print "Volume of cylinder having base area"; Area( r)
PicOutput.Print "and height"; h; "is"; h * Area (r )
End Sub
Chapter 4 - Visual Basic Schneider 57
Another Example
Private Sub cmdDisplay_Click()
Dim a As String
' Demonstrates local variables
a = “Choo “
picOutput.Print = TypeOfTrain()
End Sub
Chapter 4 - Visual Basic Schneider 58
Function TypeOfTrain
Private Function TypeOf Train( ) As String
Dim a As String
a = a & a
TypeOfTrain = a & “train”
End Function
Chapter 4 - Visual Basic Schneider 59
Last Example
Private Sub cmdDisplay_Click ( )
Dim num As Single
' Triple a number
num = 5
picOutput.Print Triple (num)
picOutput.Print num
End Sub
Chapter 4 - Visual Basic Schneider 60
Function Triple
Private Function Triple(x As Single) As Single
Dim num As Single
num = 3
Triple = num * x
End Function