Beruflich Dokumente
Kultur Dokumente
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
Property of STI
Page 1 of 25
Computer Programming 3
Types of Procedures
Sub
Function
Property of STI
Page 2 of 25
1 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
2 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Sub Procedures
Property of STI
Page 3 of 25
Computer Programming 3
Sub Procedures
Property of STI
Page 4 of 25
3 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
4 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Sub Procedures
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
Property of STI
Page 6 of 25
5 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
6 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Arguments
Property of STI
Page 7 of 25
Computer Programming 3
Parameters
Example:
Private Sub Triple(num As Single)
Property of STI
Page 8 of 25
7 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
8 __________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Passing Arguments
Property of STI
Page 9 of 25
Computer Programming 3
Passing Arguments
to Parameters
Argument
Call Triple(num)
Parameter
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
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
Property of STI
Page 12 of 25
11 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
12 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Property of STI
Page 13 of 25
Computer Programming 3
Local Variables
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
Property of STI
Page 15 of 25
Computer Programming 3
Form-Level Variables
Example:
' In Declaration section of General
Dim num1 As Single, num2 As Single
Property of STI
Page 16 of 25
15 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
16 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
What is a Function?
Property of STI
Page 17 of 25
Computer Programming 3
Types of Functions
Property of STI
Page 18 of 25
17 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
18 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Function Syntax
Property of STI
Page 19 of 25
Computer Programming 3
Function Example 1
Property of STI
Page 20 of 25
19 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
20 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Property of STI
Page 21 of 25
Computer Programming 3
Function Example 2
Property of STI
Page 22 of 25
21 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
22 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
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
Property of STI
Page 23 of 25
Computer Programming 3
Property of STI
Page 24 of 25
23 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
24 _________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
Computer Programming 3
Common Errors
Property of STI
Page 25 of 25
25 ________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________
___________________