A subroutine can call other subroutines or functions. The following is the general form of a subroutine that
calls another subroutine and a function. Again the subroutine name follows the same rules as for variables.
Note: A function or subroutine can have many parameters or no parameters.
Note: A subroutine must have an parameter for each variable input or variable output that is needed. For example
a subroutine with par6 = par4 + par5, requires three parameters.
Sub main()
instructions
variable = myFunction(a,b)
Call mysub(d,e,f)
myVar = f
End Sub
Function myFunction(par1,par2)
instructions
myFunction = par1 + par2
End Function
Sub mysub(par4,par5,par6)
instructions
par6 = par4 + par5
End Sub
Sub Test14() uses a function to add three numbers, and a subroutine to multiply three numbers. Test14() prints c = 7 and d = 10.
REM Example subroutine that calls another subroutine and a function.
Sub Test14()
Dim a As Long REM Declares a, b, c, d as integers
Dim b As Long
Dim c As Long
Dim d As Long
a = 1
b = 2
c = funcAdd(a, b) REM c equal to funcAdd(a,b), with a and b
MsgBox "c = " + c REM substituted for par1 and par2.
Call subMultiply(a, b, d) REM Calls subroutine subMultiply(a,b,d)
REM with a, b, d substituted for par3,par4,par5.
MsgBox "d = " + d
End Sub
REM Function for Test14()
Function funcAdd(par1, par2) As Long REM Function value declared.
Dim intVariable As Long REM intVariable locally defined to func.
intVariable = 4
funcAdd = par1 + par2 + intVariable REM funcAdd equals 7.
End Function
REM Sub for Test14()
Sub subMultiply(par3, par4, par5)
Dim intResult As Long REM intResult is locally defined to sub
intResult = 2 + 3
par5 = par3 * par4 * intResult REM d = par5 equal to 10.
End Sub
Sub Test14() calls a function, and a subroutine. These are the basic building blocks of all programs. Read all the comments. The function funcAdd(par1, par2) has par1 and par2 substituted with the values a and b. subMultiply() has the values a, b, d substituted into par3, par4, par5. After the calculations in subMultiply() the values of par3, par4, par5 are passed by reference back to a, b, d. (See by reference Chapter 12 Section 1.) Notice that intVariable and intResult in the procedures must be declared or you will get the error message Variable not defined. Do not declare par1, par2 in the function, and do not declare par3, par4, par5 in the subroutine or you will get the error message Duplicate declaration in current scope.
Notice if you copied all the subroutines, and functions above to the same Code Window, you may have two functions or subroutines with the same name. You must delete or change the name on one of them. If you don’t, you will get an error pop up box.
Table of Contents
Ch1-Introduction
Ch2-Loops
Ch3-If Statements
Ch4-Functions
Ch5-Subs & Functions
Ch6-Read & Write
Ch7-Operators
Ch8-Built-in Functions
Ch9-Built-in Examples
Ch10-Debugging
Ch11-Running Subs
Ch12-Sample Programs
Ch13-WS Formulas
Ch14-WS Functions
Ch15-Calc Help Page