The branch logic control refers to the process of leaving the current flow of the program to a new set of instructions. There are two types of branching logic controls: subroutines and functions. The subroutines perform actions and functions compute values. We first start with subroutines.
Subroutines are used to perform an action. An example of action might be to print a message to the screen. A subroutine starts with the keyword SUB then a meaningful and descriptive name. Next, code is written inside the subroutine before ending it with the keywords END SUB. The following shows the general syntax for creating a subroutine:
SUB SubNameStatementsEND SUBConsider the following subroutine:
SUB PrintMessageresponse.write "in subroutine<br>"END SUBThe SUB PrintMessage creates a subroutine called PrintMessage. The PrintMessage subroutine prints a message (see line 2). To print the message (or to execute code inside of any other subroutine), we have to call the subroutine. To "call" or start the code inside the subroutine, we simply write the name of the subroutine and the flow of the control goes to that subroutine. If applicable, you can also pass values to a subroutine when calling. In the following
SUB PrintMessageresponse.write "in subroutine<br>"END SUBresponse.write "Calling subroutine<br>"PrintMessageresponse.write "done calling"
the PrintMessage subroutine is called by line 5 and the subroutine prints the message "in subroutine" (see line 2). After the subroutine ends (line 3), line 6 executed. See the following output of the above code:
Arguments or variables can be passed to subroutines. The following subroutine, for example, takes one argument:
SUB PrintMessage (strMsg)response.write strMsgEND SUBPrintMessage ("Passing a string argument to the PrintMessage subroutine.")The PrintMessage subroutine above takes one argument, strMsg. We pass the string argument when we call the PrintMessage subroutine on line 4.
You can also pass more than one argument by separating the arguments by a comma. When you pass argument(s) to a subroutine make sure the order and type of arguments is correct. For instance, if your subroutine expects three arguments as such that the first argument needs to be date, the second as string, the third as an integer, make sure that your first argument is date, the second a string, and third an integer value.
In the following
PrintMessage strMessage, numOfTimesToPrint
we pass two arguments to a subroutine named PrintMessage. The first argument is a string and the second argument is an integer, as shown below:
SUB PrintMessage (strMsg, numPrint)for i = 0 to numPrintresponse.write strMsg & "<br>"nextEND SUBdim strMessage, numOfTimesToPrintstrMessage = "Argument passing to a subroutine."numOfTimesToPrint = 5PrintMessage strMessage, numOfTimesToPrintThis subroutine prints "Argument passing to a subroutine." 5 times with a for loop (see lines 2 through 4). The following shows the output of the above code:
Functions are written to perform calculations. Like subroutines, functions can take one or more arguments. Functions can also return one value. A function begins with the keyword FUNCTION and ends with the keyword END FUNCTION. The following shows the general syntax for defining a function:
FUNCTION functionNameStatementsEND FUNCTIONAs an example, we will write a function that will return a positive number if we pass a negative number. In the following function definition,
FUNCTION AbsoluteValue (intNumber)If intNumber < 0 thenintNumber = - intNumberEND IFAbsoluteValue = intNumberEND FUNCTION
our function is named AbsoluteValue and takes one argument. On line 2, we check if the value of the variable intNumber is less than 0, if it is, then we change the negative sign of the number (value) to a positve number. For example, if we pass -100 to this function, third line above will set intNumber to -(-100), which is 100. The line 5 (AbsoluteValue = intNumber) shows how to return a value from a function. To return a value from a function, simply set the name of the function to the value you want to return. The value that is returned by a function can be printed or assigned to another variable for later use in the program.
In the following, we use the above function:
FUNCTION AbsoluteValue (intNumber)If intNumber < 0 thenintNumber = - intNumberEND IFAbsoluteValue = intNumberEND FUNCTIONDIM i, intFuntionReturnsi = -9DO WHILE (i <= 6)intFuntionReturns = AbsoluteValue (-i)response.write "Value passed: " & i & ". "response.write "Value returned: " & intFuntionReturns & "<br>"i = i + 3LOOPTo this function, we use a DO WHILE loop to pass different values between -9 and 6. In the loop, we first -9 to our function. The function returns back 9. The second time the loop executes, we pass -6 and the function returns 6. The following shows the output of the above code: