FUNCTION statement

Top  Previous  Next

Function [ (parameterlist) ] [ As returntype ]

   [ statements ]

   [ Exit Function ]

   [ Return value ]

   [ statements ]

End Function

 

Declares the name, parameters and code that define a Function procedure.

 

The parameter list contains one or more variables separated by a comma, the datatype must not be declared. Parameters by reference must be declared by using the BYREF directive.

 

To return a value from a function, you have to assign the value to the function name. You can also use the return statement with a result value to exit the function, eg. "return i"

 

Use a FUNCTION statement when you need to return a value to the calling code. Use a SUB statement when you do not need to return a value.

 

Example:

Function UseMax(a, b) As Integer

   If a > b Then

      UseMax = a

   Else

      UseMax = b

   End If

End Function

 

' **********

 

Function UseMax2(a, b) As Integer

   If a > b Then

      return a

   Else

      return b

   End If

End Function