Ch 7 Procedures (Subroutines in Visual Basic) Visual Basic Procedures Structured Programs To simplify writing complex programs, most Programmers (Designers/Developers) choose to split the problem into simpler smaller tasks (modules). They then solve each small task before linking the 'modules' together as a complete program. The modules, being smaller and simpler, are easier to design, write and test. The final program is made by combining individually tested modules, so that final checking consists of making sure that the modules work together as intended. Advantages to structuring a program in this way are that it aids readability when designing or amending. It also reduces the size of the program (a module is written once and can be used in several parts of a program and even included in other programs). Modules /Subroutines/Procedures A module is also called a subroutine or a procedure. All BASIC code for forms and control objects are contained in modules, but (as explained above) a complex task can be simplified by breaking it down into smaller steps and tackling each step as a separate module. A subroutine is created in BASIC as follows: (looks similar to a Form or object event procedure) SUB subname (parameter,...) END SUB : : : statements : : : subname parameter - the name of the subroutine. Used to call the subroutine from the main program. - one or more variables passed to the subroutine from the main program. ByVal The value of the variable is passed to the procedure. This mean that the value in the main program is not changed. ByRef The memory location is passed to the procedure. This means that the value in the main program IS changed. Ch7-Procedures WED Section 7-1
Example This is a trivial problem but serves to demonstrate the use of a subroutine within an event procedure Sub Main() Dim FirstNum As Integer Dim SecondNum As Integer Console.WriteLine("Test Max()") Console.Write("Enter First Number :") FirstNum = Console.ReadLine() Console.Write("Enter Second Number :") SecondNum = Console.ReadLine() Max (FirstNum, SecondNum) Console.ReadKey() The Main program Calls the procedure Max with the two numbers as the parameters. --------------------------------------------------------------- Sub Max(ByVal n1 As Integer, ByVal n2 As Integer) If N1 > n2 Then Console.WriteLine("{0} is greater.", n1) Else Console.WriteLine("{0} is greater.", n2) End If The procedure Receives FirstNum as n1 & SecondNum as n2 Program Run Test Max ( ) Enter First Number : 10 Enter Second Number : 5 10 is greater The program accepts two numbers entered from the user and sends them to the Subroutine Max() in its parameters. Max() then decides which number is greater and displays it on screen. Note that Max receives its parameters ByVal (see previous page) Also the received variables do not have to have the same name as their name in the main program. This is a simple example of a Procedure but that s how it works folks! Exercise 7.1 1) Create the program above & test it. 2) Add a new function called Min to display the lowest value. 3) What if the two values are equal? Ch7-Procedures WED Section 7-2
An Example using ByRef. In this example the parameters are passed ByRef. The procedure SWAP receives two variables in its parameter and swaps them into order (Highest & lowest). When the main program writes the variables the swap routine has put them into order. Sub Main() Dim FirstNum As Integer Dim SecondNum As Integer Console.WriteLine("Test Swap()") Console.Write("Enter First Number :") FirstNum = Console.ReadLine() Console.Write("Enter Second Number :") SecondNum = Console.ReadLine() Swap(FirstNum, SecondNum) Console.WriteLine("Highest value is {0}", FirstNum) Console.WriteLine("Lowest value is {0}", SecondNum) Console.ReadKey() ------------------------------------------ Sub Swap(ByRef n1 As Integer, ByRef n2 As Integer) Dim temp As Integer If n1 < n2 Then temp = n1 n1 = n2 n2 = temp End If The parameters are received as ByRef so the changes made to the variables will be taken back into the main program. Temp is a local variable Program Run: Test Swap() Enter First Number : 10 Enter Second Number : 5 Highest value is 10 Lowest value is 5 Exercise 7.2 1) Create the program above and test it 2) Change the data types to string and get the swap() procedure to sort into alphabetical order. Ch7-Procedures WED Section 7-3
Further Example As a further example consider the following procedure that determines the grade awarded to a student. The procedure has two parameters: Mark is declared as Integer to receive the students mark (ByVal) Grade is declared as Char to return the grade (ByRef) SUB CalcGrade (ByVal Mark as Integer, ByRef Grade as Char) IF Mark > 75 THEN Grade = "Dist" ELSEIF Mark > 60 THEN Grade = "Merit" ELSEIF Mark > 45 THEN Grade = "Pass" ELSEIF Mark > 35 THEN Grade = "Ref" ELSE Grade = "Fail" END SUB A series of IF...THEN statements test the student Mark and set the grade accordingly. When the procedure has completed the appropriate grade is returned through the parameter to the main program. The call instruction could take the form: CalcGrade (TotalMark, Grade) Exercise 7.3 1) Write a program that uses the CalcGrade procedure to determine the grade for a student. Ch7-Procedures WED Section 7-4
Global & Local Variables A Procedure can be thought of as a small program in its own right. It creates a memory block of its own and (as well as the parameters it receives) may create its own variables if needed. The variables declared (DIM) within a procedure are called local variables and are used in that procedure only (i.e. local to that procedure). Variables declared in the main program are called global variables and can be used within the main program and any procedure or function within the module. It is not advisable to change a global variable within a procedure or function unless the variable has been passed as a parameter using ByRef. Scope Scope defines the visibility of a variable. Variables can have scope ranging from global where any procedure in the application can access the variable to local used only in a single procedure. There are three types of scope for variables in Visual Basic: Global Scope -Global variables are in scope anywhere in your application. Module Scope -Module level variables are in scope anywhere within the module where they are declared. Local Scope -Local variables are only in scope within the procedure where they are declared. Variables may be declared as follows: Term Public Private Visibility Anywhere in or outside of a project Only in the block where defined Ch7-Procedures WED Section 7-5