Sub Procedures and Functions Visual Basic
Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write Sub Procedures Compare between Call by Value and Call by Reference
Visual Basic Sub Procedures Slide 3 of 26 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 Call By Reference sub procedure
Visual Basic Sub Procedures Slide 4 of 26 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: nCall nBy Ref nBy Val
Visual Basic Sub Procedures Slide 5 of 26 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. Without Breaking problems down into smaller modules will increase program maintenance cost.
Visual Basic Sub Procedures Slide 6 of 26 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 programs
Visual Basic Sub Procedures Slide 7 of 26 Different Modular Techniques Sub Procedures Function Procedures Event Procedures Visual Basic provides us with the ability to represent modules through the use of the following:-
Visual Basic Sub Procedures Slide 8 of 26 Sub Procedures Sub Procedures are subprograms or routines which are self contained and are used to perform a particular task. What are Sub Procedures?
Visual Basic Sub Procedures Slide 9 of 26 Writing a Sub Procedure Private Sub cmdAdd_click() Call add(2,1) End sub Private sub add(num1,num2 as integer) Dim total as integer total = num1 + num2 End Sub Arguments parameters
Visual Basic Sub Procedures Slide 10 of 26 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 Quick Review Question
Visual Basic Sub Procedures Slide 11 of 26 Answer Private Sub cmdmultiply_Click() Dim a,b as integer 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
Visual Basic Sub Procedures Slide 12 of 26 No Arguments – No return value MAIN PROGRAM Private Sub Command1_Click( ) ‘ Check the parenthesis. There is no argument Call No_arg_No_return End Sub No arguments
Visual Basic Sub Procedures Slide 13 of 26 No Arguments – No return value SUB PROCEDURE Public Sub No_arg_No_return() Dim x, y, z As Integer x = 20 y = 30 z = x * y Label1.Caption = "TOTAL = " & z End Sub
Visual Basic Sub Procedures Slide 14 of 26 OUTPUT
Visual Basic Sub Procedures Slide 15 of 26 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. Quick Review Question
Visual Basic Sub Procedures Slide 16 of 26 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(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 lblSmallest.Caption = "Smallest value is " & min End Sub
Visual Basic Sub Procedures Slide 17 of 26 Call By Reference 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 Sub Procedures Slide 18 of 26 Call By Reference - Procedure 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 Sub Procedures Slide 19 of 26 Call By Value When you pass a variable to a sub program and it does not affect the value from the main program
Visual Basic Sub Procedures Slide 20 of 26 Call By Value 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 Sub Procedures Slide 21 of 26 Group Exercise 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 A B C D 0 – 39 F
Visual Basic Sub Procedures Slide 22 of 26 n Write a program that uses a Sub procedure Maximum to determine the largest of three Integers. The smallest value is displayed in a Label. Follow Up Assignment
Visual Basic Sub Procedures Slide 23 of 26 Module Design – Sub Procedures – Function Procedures – Event Procedures Sub Procedures – Call By Value – Call By Reference Summary of Main Teaching Points