Title: Visual Basic 'Net AAPP00832 VBN
1 Visual Basic .NetAAPP00-8-3-2 VBN
Sub Procedures Lecture 8
2Topic Structure of the lesson
- Introduction to Modular Design Concepts
- Write Sub Procedures
- Compare between Call by Value and Call by
Reference
3Learning Outcomes
- At the end of this lecture you should be able to
- Declare a sub program
- Write a sub program and pass arguments to it
- Write a Call-By-Value and a Call By Reference sub
procedure
4Key Terms you must be able to use
- If you have mastered this topic, you should be
able to use the following terms correctly in your
assignments and tests - Call By Ref
- Call By Val
5Modular Design Concepts
Modularity refers to breaking a large problem
down into smaller self-contained modules. This
allows the programmer to isolate problems within
the program by looking at specific
areas. Breaking problems down into smaller
modules, will decrease program maintenance cost.
6Dividing code into procedures
- Divide Conquer Approach
- Makes program development more manageable.
- Software Reusability
- Using existing procedures as building blocks
for new programs - Avoid duplicating parts of programs
7Different Modular Techniques
Visual Basic provides us with the ability to
represent modules through the use of the
following
-
- Sub Procedures
- Function Procedures
- Event Procedures
8Sub Procedures
What are Sub Procedures?
Sub Procedures are subprograms or routines which
are self contained and are used to perform a
particular task.
9Breaking down a problem into smaller parts
- Private Sub btnDrawSnowman_Click()
- Dim small, medium, large, left, right as
integer - small 1
- medium 2
- large 3
- call drawCircle(small)
- call drawCircle(medium)
- call drawCircle(large)
-
call drawHat() left 1 right 2 call
drawArm(left) call drawArm(right) End Sub
10A Sub Program with No Arguments
MAIN PROGRAM Private Sub Command1_Click( )
Check the parentheses. There is no argument
Call No_arg_No_return( ) End Sub
No arguments
11No Arguments
SUB PROCEDURE Public Sub No_arg_No_return( )
Dim x, y, z As Integer x 200 y 400 z
x y Label1.Caption "TOTAL " z End
Sub
12OUTPUT
13A Sub Procedure with Arguments
- Private Sub cmdAdd_click()
- Call max(2,1)
- End sub
- Private Sub max(num1, num2 as integer)
- Dim max as integer
- if num1 gt num2 then
- max num1
- elseif num2 gt num1 then
- max num2
- Else
- MessageBox.Show (The numbers are equal)
- End If
- End Sub
parameters
14Exercise 1
Write a program to accept two numbers using
textboxes and pass the numbers to a subprogram
called product to multiply them. Display the
answer in the product procedure. Use a
MessageBox to display the answer
15Answer
- Private Sub cmdMultiply_Click()
- Dim a,b as integer
- a InputBox(enter 1st number)
- b InputBox(enter 2nd number)
- Call product (a, b)
- End Sub
- Private Sub product (num1 As integer, num2 As
integer) - MsgBox ("The product of num1 and
num2 is
num1 num2) - End Sub
16Exercise 2
- Write a program that will accept three numbers
and pass the numbers to a procedure minimum to
determine the smallest number. - Accept the numbers using an InputBox and display
the smallest number in a Text Box.
17Solution
- Private Sub cmdSmallest_Click()
- Dim value1 As Long, value2 As Long, value3 As
Long - value1 txtOne.Text
- value2 txtTwo.Text
- value3 txtThree.Text
- Call Minimum(value1, value2, value3)
- End Sub
- Private Sub Minimum(x As Long, y As Long, z As
Long) - dim min as Long
- min x
- If y lt min Then
- min y
- End If
- If z lt min Then
- min z
- End If
- lblSmallest.Caption "Smallest value is "
min - End Sub
18 EXIT Sub
- EXIT SUB will alter the flow of control in your
program - Executing EXIT SUB in a sub procedure causes an
immediate exit from that procedure.
19EXIT SUB Example
Private Sub cmdBegin_Click() Dim x As Integer For
x 5 To -1 Step -1 Call PrintNumbers(x) Nex
t cmdBegin.Enabled False End Sub
20EXIT SUB Example
- Private Sub PrintNumbers(number As Integer)
- If number gt0 Then
- Print number
- Else
- Print "Exiting Sub with number "
number - Exit Sub
- End If
- End Sub
21Call By Reference
- If a variable retains the value it is given in a
sub procedure, it is passed by reference - By default Call By Reference is used in VB (FYI
This is not true in C and Java.)
22Call By Reference - Procedure
- Private sub a()
- dim a as integer
- a 5
- call square(a)
- MessageBox.Show (Ans a a)
- end sub
- private sub square(num as integer)
- num num num
- end sub
- Ans a 25
23Call By Value
- When you pass a copy of the variable to a sub
program - it does not affect the value in the
main program
24Call By Value
- Private sub a()
- dim a as integer
- a 5
- call square(a)
- MessageBox.Show (Ans a a)
- end sub
- private sub square (byval num as integer)
- num num num
- end sub
- Ans a 5
25 Exercise 3
- Write a program that accepts a student mark and
passes the mark to a subprogram called grade
which will determine the students grade. - Mark Grade
- 75-100 A
- 60-74 B
- 50- 59 C
- 40-49 D
- 0 39 F
26Exercise 4
- Write a program that uses a Sub procedure Maximum
to determine the largest of three Integers. The
smallest value is displayed in a Label.
27Summary of Main Teaching Points
- Module Design
- Sub Procedures
- Function Procedures
- Event Procedures
- Sub Procedures
- Do not return a value
- Call By Value
- Call By Reference
28Next Session
29Question and Answer Session
Q A