Branch logic controls

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.

Using 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 SubName
Statements
END SUB

Consider the following subroutine:

SUB PrintMessage
response.write "in subroutine<br>"
END SUB

The 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 PrintMessage
response.write "in subroutine<br>"
END SUB
response.write "Calling subroutine<br>"
PrintMessage
response.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:

Calling subroutine
in subroutine
done calling

Argument passing

Arguments or variables can be passed to subroutines. The following subroutine, for example, takes one argument:

SUB PrintMessage (strMsg)
response.write strMsg
END SUB
PrintMessage ("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 numPrint
response.write strMsg & "<br>"
next
END SUB
dim strMessage, numOfTimesToPrint
strMessage = "Argument passing to a subroutine."
numOfTimesToPrint = 5
PrintMessage strMessage, numOfTimesToPrint

This 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:

Argument passing to a subroutine.
Argument passing to a subroutine.
Argument passing to a subroutine.
Argument passing to a subroutine.
Argument passing to a subroutine.
Argument passing to a subroutine.

Using functions

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 functionName
Statements
END FUNCTION

As 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 then
intNumber = - intNumber
END IF
AbsoluteValue = intNumber
END 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 then
intNumber = - intNumber
END IF
AbsoluteValue = intNumber
END FUNCTION

DIM i, intFuntionReturns
i = -9
DO WHILE (i <= 6)
intFuntionReturns = AbsoluteValue (-i)
response.write "Value passed: " & i & ". "
response.write "Value returned: " & intFuntionReturns & "<br>"
i = i + 3
LOOP

To 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:

Value passed: -9. Value returned: 9
Value passed: -6. Value returned: 6
Value passed: -3. Value returned: 3
Value passed: 0. Value returned: 0
Value passed: 3. Value returned: 3
Value passed: 6. Value returned: 6