Sie sind auf Seite 1von 7

Computer Programming 3

What is
Modularization?
breaks the program into subtasks
Sub procedure or Function performs a welldefined task
makes programs easier to test, debug and
maintain
provides abstract operations

Sub Procedures and Functions

Property of STI
Page 1 of 25

Computer Programming 3

Types of Procedures

In Visual Basic, there are two types of


procedures:

Sub
Function

To distinguish procedures from event


procedures, Sub and Functions are referred
to as general procedures.

Sub Procedures and Functions

Property of STI
Page 2 of 25

1 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
2 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Sub Procedures

Private Sub ProcedureName ( )


statement(s)
End Sub

Sub Procedures and Functions

Property of STI
Page 3 of 25

Computer Programming 3

Sub Procedures

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 its parameters

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.

Sub Procedures and Functions

Property of STI
Page 4 of 25

3 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
4 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Sub Procedures

Activate the code window


Select Add Procedure from the Tools menu
Type in the name of the Sub procedure

Select Sub from the Type box


Click on Private in Scope frame
Press the ENTER key or click the OK button
Type the statements of the Sub procedure

Sub Procedures and Functions

Property of STI
Page 5 of 25

Computer Programming 3

Sub Procedure
Call Example
Private Sub cmdCompute_Click()
Dim num As Single
num = Val(InputBox("Enter a
number:"))
Call Triple(num)

End Sub

Private Sub Triple(num As Single)

'Multiply the value of the number by


3
picResult.Print "The number is"; 3 *
num
End Sub

Sub Procedures and Functions

Property of STI
Page 6 of 25

5 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
6 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Arguments

Arguments : Variables or expressions placed


in parentheses in a Call statement.
The Call statement causes a Sub procedure
to be executed.
Example:
Call Triple(num)

Sub Procedures and Functions

Property of STI
Page 7 of 25

Computer Programming 3

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.

Example:
Private Sub Triple(num As Single)

Sub Procedures and Functions

Property of STI
Page 8 of 25

7 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
8 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

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.
A Sub procedure can receive many
values

A Sub procedure can return many


values
Sub Procedures and Functions

Property of STI
Page 9 of 25

Computer Programming 3

Passing Arguments
to Parameters

Argument
Call Triple(num)

Private Sub Triple (num As Single)

Parameter

Sub Procedures and Functions

Property of STI
Page 10 of 25

9 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
10 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Passing Arguments
to Parameters

Arguments
Call Add (x, y )
Private Sub Add ( num1 As Single, num2 As
Single)

Parameters

Sub Procedures and Functions

Property of STI
Page 11 of 25

Computer Programming 3

Passing Arguments
to Parameters Example
Private Sub cmdDisplay_Click()
Dim amt As Single
amt = 2
picResults.Print amt;
Call Triple(amt)
picResults.Print amt
End Sub
Sub Triple
Private Sub Triple(num As Single)
' Triple a number
picResults.Print num;
num = 3 * num
picResults.Print num;
End Sub

Sub Procedures and Functions

Property of STI
Page 12 of 25

11 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
12 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Rules for Passing


Arguments to a Sub
Values can be passed between the calling
program and Sub by passing arguments.
The number of arguments and parameters
must match.
The data type of each argument must match
its corresponding parameter.
Variables that are defined in a particular Sub
are local variables.

Sub Procedures and Functions

Property of STI
Page 13 of 25

Computer Programming 3

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 the variable has
been defined

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

Sub Procedures and Functions

Property of STI
Page 14 of 25

13 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
14 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Local Variables
Example
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
Sub Add
Private Sub Add(num As Integer)
Dim var1 As Integer, var2 As
Integer
num = var1 + var2
End Sub

Sub Procedures and Functions

Property of STI
Page 15 of 25

Computer Programming 3

Form-Level Variables

Form-level variables are visible to every


procedure (Global variable).
Form-level variables appear at the top of
the code window.
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

Example:
' In Declaration section of General
Dim num1 As Single, num2 As Single

Sub Procedures and Functions

Property of STI
Page 16 of 25

15 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
16 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

What is a Function?

designed to perform a specific task


designed to return a single value to the
calling program

Sub Procedures and Functions

Property of STI
Page 17 of 25

Computer Programming 3

Types of Functions

Standard functions (built-in)


User-defined functions

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.

Sub Procedures and Functions

Property of STI
Page 18 of 25

17 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
18 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Function Syntax

Private Function FunctionName


(parameter-list) As datatype
Statement(s)
..
FunctionName = ..
End Function

Sub Procedures and Functions

Property of STI
Page 19 of 25

Computer Programming 3

Function Example 1

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

Sub Procedures and Functions

Property of STI
Page 20 of 25

19 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
20 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Defining and Calling


a Function
User-defined function must include a
statement that assigns a value to the
function name.
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.
A function can receive many values

Only one value can be directly


returned
Sub Procedures and Functions

Property of STI
Page 21 of 25

Computer Programming 3

Function Example 2

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

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

Sub Procedures and Functions

Property of STI
Page 22 of 25

21 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
22 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Sub and Function


Example
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

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

Sub Procedures and Functions

Property of STI
Page 23 of 25

Computer Programming 3

Sub and Function


Example (cont.)
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

Sub Procedures and Functions

Property of STI
Page 24 of 25

23 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
24 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Computer Programming 3

Common Errors

Passing incorrect data types


Not specifying the data type of the returned
value
Forgetting the data type of a function's
parameter
Not assigning a value to the function name
inside the function definition
Misspelling of the Function name
Incorrect calling of the function in an
expression

Sub Procedures and Functions

Property of STI
Page 25 of 25

25 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________

Das könnte Ihnen auch gefallen