Executing a user-defined function

The way you execute a user-defined function depends on the number of arguments that the function expects to be passed when you call it and whether the function appears as part of a statement (such as an assignment statement or a Print statement) or just by itself.

Executing a function that takes no arguments

When you call a parameterless function by including it in a statement, the function name can end in empty parentheses or no parentheses.

For example:

Dim anInt As Integer
Dim aDouble As Double
Function Cubit1 As Double
   ' Return the cube of anInt% and display a message
   ' saying what that value is. 
   Cubit1# = anInt% ^ 3
   Print anInt% & " cubed = " & Cubit1# & "."
End Function
anInt% = 4
aDouble# = Cubit1#
' Output:  4 cubed is 64.  
aDouble# = Cubit1#
' Output: 4 cubed is 64.
Print aDouble#
' Output: 64
Print Cubit1#
' Output: 4 cubed is 64.
          64

You can call a parameterless function by entering the function name, which must not include empty parentheses.

For example:

Cubit1#
' Output: 4 cubed is 64

Executing a function that takes a single argument

When you call a function that expects a single argument, you must enclose that argument in parentheses when you include the function in a statement.

For example:

Dim anInt As Integer
Dim aDouble As Double
Function Cubit2(X As Integer) As Double
   ' Return the cube of X% and display a message
   ' saying what that value is.
   Cubit2# = X% ^ 3
   Print X% & " cubed = " & Cubit2# & "."
End Function
anInt% = 4
aDouble# = Cubit2#(anInt%)
' Output: 4 cubed is 64. 
Print aDouble#
' Output: 64  
Print Cubit2#(anInt%)
' Output: 4 cubed is 64.
          64  

You can call a one-parameter function in any of the following additional ways:

  • With a Call statement. You must enclose the argument in parentheses.
  • By entering the name of the function followed by the argument that it expects with no parentheses.
  • By entering the name of the function followed by the argument it expects enclosed in parentheses. This notation means that you are passing the argument by value rather than by reference.

For example:

Call Cubit2#(anInt%)
' Output: 4 cubed is 64. (anInt% is passed by reference.)
Cubit2# anInt%
' Output: 4 cubed is 64. (anInt% is passed by reference.) 
Cubit2#(anInt%)
' Output: 4 cubed is 64. (anInt% is passed by value.)

Executing a function that takes multiple arguments

When you call a function that expects multiple arguments, you must enclose those arguments in parentheses when you include the function in a statement.

For example:

Dim anotherInt As Integer
Function Cubit3(X As Integer, Y As Integer) As Double
    ' Return the product of X% and Y%.
    Cubit3# = X% * Y%
    Print X% & " times " Y% & " = " & Cubit3# & "."
End Function
anInt% = 4
anotherInt% = 6
Print Cubit3#(anInt%, anotherInt%)
' Output: 4 times 6 = 24.
          24

You can also call a function that expects multiple arguments with a Call statement or by entering the function name followed by the arguments. The Call statement requires parentheses; the function name by itself does not allow parentheses.

For example:

Call Cubit3#(anInt%, anotherInt%)
' Output: 4 times 6 = 24.
Cubit3# anInt%, anotherInt%
' Output: 4 times 6 = 24.

Executing a function recursively

A recursive function is a function that calls itself. A call to itself from within the function is called a recursive call.

The definition of a recursive function must provide a way to end the recursion.

The depth of recursion is limited by a 32K byte stack size.

When recursively calling a function that has no arguments, you must insert empty parentheses following the function name in the call if you use the function's return value. The parentheses show that the function is being called. The function name without parentheses is interpreted as the variable that represents the return value of the function.

Example 1

Function Facto# (theNum%)
   ' Calculate theNum% factorial and make it 
   ' the return value of Facto#.             
   If theNum% <= 0 Then
      Facto# = 0
   ElseIf theNum% = 1 Then
      Facto# = 1
   Else
      Facto# = theNum% * Facto#(theNum% -1)
   End If
End Function

Example 2

This example shows a recursive function without arguments:

Function Recurse As Integer
   ' ...
   ' Call Recurse and assign the return value to x.
   x = Recurse()
   ' ...
   ' Assign the current value of the Recurse variable to x.
   x = Recurse  
   ' ...
End Function