Lec 08 Subprocedures

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

Visual Basic .

Net
AAPP00-8-3-2 VBN

Sub Procedures
Lecture 8
Topic & Structure of the lesson

•Introduction to Modular Design Concepts


•Write Sub Procedures
•Compare between Call by Value and Call
by Reference

AAPP00-8-3-2 VBN Visual Basic .Net Slide 2 (of 25)


Learning Outcomes

At the end of this lecture you should be able to :


1. Declare a sub program
2. Write a sub program and pass
arguments to it
3. Write a Call-By-Value and a Call By
Reference sub procedure

AAPP00-8-3-2 VBN Visual Basic .Net Slide 3 (of 25)


Key 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 4 (of 25)


Modular 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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 5 (of 25)


Dividing 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 6 (of 25)


Different Modular Techniques

Visual Basic provides us with the ability to


represent modules through the use of the
following:

• Sub Procedures
• Function Procedures
• Event Procedures

AAPP00-8-3-2 VBN Visual Basic .Net Slide 7 (of 25)


Sub Procedures

What are Sub Procedures?

Sub Procedures are subprograms or routines


which are self contained and are used to
perform a particular task.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 8 (of 25)


Breaking 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 drawHat()
call drawCircle(medium) left = 1
call drawCircle(large) right = 2
call drawArm(left)
call drawArm(right)
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net


A 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 12 (of 25)


No 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 13 (of 25)


OUTPUT

AAPP00-8-3-2 VBN Visual Basic .Net Slide 14 (of 25)


A Sub Procedure with Arguments
Private Sub cmdAdd_click()
Call max(2,1) Arguments
End sub
Private Sub max(num1, num2 as integer)
Dim max as integer
if num1 > num2 then
max = num1
parameters
elseif num2 > num1 then
max = num2
Else
MessageBox.Show (“The numbers are equal”)
End If
End Sub
AAPP00-8-3-2 VBN Visual Basic .Net Slide 9 (of 25)
Exercise 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 10 (of 25)


Answer
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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 11 (of 25)


Exercise 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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 15 (of 25)


Solution
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 < min Then
min = y
End If
If z < min Then
min = z
End If
txtSmallest.Text = "Smallest value is " & min
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 16 (of 25)


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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 16 (of 30)


EXIT SUB Example

Private Sub cmdBegin_Click()


Dim x As Integer
For x = 5 To -1 Step -1
Call PrintNumbers(x)
Next
cmdBegin.Enabled = False
End Sub

AAPP00-8-3-2 VBN Visual Basic .Net Slide 17 (of 30)


EXIT SUB Example

Private Sub PrintNumbers(number As Integer)


If number >=0 Then
Print number
Else
Print "Exiting Sub with number = " &
number
Exit Sub
End If
End Sub
AAPP00-8-3-2 VBN Visual Basic .Net Slide 18 (of 30)
Call 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.)

AAPP00-8-3-2 VBN Visual Basic .Net Slide 17 (of 25)


Call 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 18 (of 25)


Call By Value

• When you pass a copy of the variable to


a sub program - it does not affect the
value in the main program

AAPP00-8-3-2 VBN Visual Basic .Net Slide 19 (of 25)


Call 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 20 (of 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 21 (of 25)


Exercise 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.

AAPP00-8-3-2 VBN Visual Basic .Net Slide 22 (of 25)


Summary 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

AAPP00-8-3-2 VBN Visual Basic .Net Slide 23 (of 25)


Next Session

Function Procedures

AAPP00-8-3-2 VBN Visual Basic .Net Slide 25 (of 25)


Question and Answer Session

Q&A

AAPP00-8-3-2 VBN Visual Basic .Net Slide 24 (of 25)

You might also like