Visual Basic Sub Procedures and Functions Topic Structure

Visual Basic Sub Procedures and Functions

Topic & Structure of the lesson Sub Procedures • Introduction to Modular Design Concepts • Write Sub Procedures • Compare between Call by Value and Call by Reference Visual Basic 2

Learning Outcomes Sub Procedures 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 Call By Reference sub procedure Visual Basic 3

Key Terms you must be able to use Sub Procedures If you have mastered this topic, you should be able to use the following terms correctly in your assignments and tests: n Call n By Ref n By Val Visual Basic 4

Modular Design Concepts Sub Procedures 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. Without Breaking problems down into smaller modules will increase program maintenance cost. Visual Basic 5

Dividing code into procedures Sub Procedures • Divide & Conquer Approach Makes program development more manageable. • Software Reusability Using existing procedures as building blocks for new programs • Visual Basic Avoid Duplicating programs 6

Different Modular Techniques Sub Procedures Visual Basic provides us with the ability to represent modules through the use of the following: - • Sub Procedures • Function Procedures • Event Procedures Visual Basic 7

Sub Procedures What are Sub Procedures? Sub Procedures are subprograms or routines which are self contained and are used to perform a particular task. Visual Basic 8

Writing a Sub Procedures Private Sub cmd. Add_click() Arguments Call add(2, 1) End sub Private sub add(num 1, num 2 as integer) Dim total as integer total = num 1 + num 2 parameters End Sub Visual Basic 9

Quick Review Question Sub Procedures Write a program to accept two numbers using textboxes and pass the numbers into a subprogram called product to multiply them. Display the answer in the product procedure. Use a Msgbox to display the answer Visual Basic 10

Answer Sub Procedures Private Sub cmdmultiply_Click() Dim a, b as integer Call product (a, b) End Sub Private Sub product (num 1 As integer, num 2 As integer) Msgbox ("The product of “ & num 1 & "and“ & num 2 & "is“ & num 1 * num 2) End Sub Visual Basic 11

No Arguments – No return value Sub Procedures MAIN PROGRAM Private Sub Command 1_Click( ) ‘ Check the parenthesis. There is no argument Call No_arg_No_return End Sub No arguments Visual Basic 12

No Arguments – No return value Sub Procedures SUB PROCEDURE Public Sub No_arg_No_return() Dim x, y, z As Integer x = 20 y = 30 z=x*y Label 1. Caption = "TOTAL = " & z End Sub Visual Basic 13

OUTPUT Sub Procedures Visual Basic 14

Quick Review Question Sub Procedures Write a program that will accept three numbers and pass the numbers to a procedure minimum to determine the smallest number. Accept the three numbers using input box and display the smallest number on a text box. Visual Basic 15

Solution Sub Procedures Private Sub cmd. Smallest_Click() Dim value 1 As Long, value 2 As Long, value 3 As Long value 1 = txt. One. Text value 2 = txt. Two. Text value 3 = txt. Three. Text Call Minimum(value 1, value 2, value 3) End Sub Private Sub Minimum(min As Long, y As Long, z As Long) If y < min Then min = y End If If z < min Then min = z End If lbl. Smallest. Caption = "Smallest value is " & min End Sub Visual Basic 16

Call By Reference Sub Procedures • When you pass a variable to a sub procedure and return its updated value • By default Call By Reference is used in VB Visual Basic 17

Call By Reference - Procedure Sub Procedures Private sub a() dim a as integer a=5 call square(a) print a end sub private sub square(num as integer) num = num * num end sub Ans a = 25 Visual Basic 18

Call By Value Sub Procedures • When you pass a variable to a sub program and it does not affect the value from the main program Visual Basic 19

Call By Value Sub Procedures Private sub a() dim a as integer a=5 call square(a) print a end sub private sub square (byval num as integer) num = num * num end sub Ans a = 5 Visual Basic 20

Group Exercise Sub Procedures • 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 60 -74 50 - 59 40 -49 0 – 39 Visual Basic A B C D F 21

Follow Up Assignment Sub Procedures n Visual Basic Write a program that uses a Sub procedure Maximum to determine the largest of three Integers. The smallest value is displayed in a Label. 22

Summary of Main Teaching Points Sub Procedures • Module Design – Sub Procedures – Function Procedures – Event Procedures • Sub Procedures – Call By Value – Call By Reference Visual Basic 23
- Slides: 23