FUNCTION
Source: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgFunction Last revised: 2023-08-30
Defines a procedure that returns a value.
Syntax
[Public|Private] Function identifier [CDecl|Pascal|StdCall] [Overload] [Alias "external_identifier"] [([parameter_list])] [ByRef] As return_type [Static] [Export]
statements
...
{ {Return [return_value]}|{Function = return_value}|{identifier = return_value} }
...
End FunctionParameters
- identifier — Function name.
- external_identifier — Externally visible name for linker, enclosed in quotes.
- parameter_list —
parameter[, parameter[, ...]][ByRef|ByVal] identifier [As type] [= default_value]- Array parameters: identifier followed by
()— always ByRef.
- return_type — The data type of the return value.
- return_value — The value to return.
Description
A function defines a block of code that can be called with a single statement and returns a value to the caller.
Access: Public and Private specify module-level access. Default is Public.
Calling Conventions: CDecl, Pascal, StdCall specify argument push/pop order. Default is StdCall.
Return Value: Three methods to return a value:
Return value— immediately exits and returns the value.Function = value— sets return value but does not exit.identifier = value— sets return value but does not exit.
A function can also return a reference with ByRef As return_type.
Warning: A return value must always be defined, otherwise unexpected behavior may occur.
Overloading: The Overload keyword allows a function to share its name with other functions having different signatures.
Static: Local variables preserve their values between calls when Static is used at procedure level.
Examples
Example 1: Using Return
Declare Function ReturnTen () As Integer
Print ReturnTen ()
Function ReturnTen() As Integer
Return 10
End FunctionExample 2: Using assignment to function name
Function ReturnTen() As Integer
ReturnTen = 10
End FunctionExample 3: Function overloading
Declare Function ReturnTen Overload (a As Single) As Integer
Declare Function ReturnTen Overload (a As String) As Integer
Declare Function ReturnTen (a As Integer) As Integer
Print ReturnTen (10.000!)
Print ReturnTen (10)
Print ReturnTen ("10")
Function ReturnTen Overload (a As Single) As Integer
Return Int(a)
End Function
Function ReturnTen Overload (a As String) As Integer
Return Val(a)
End Function
Function ReturnTen (a As Integer) As Integer
Return a
End FunctionExample 4: Optional parameters
Function TestFunc(P As String = "Default") As String
Return P
End Function
Print TestFunc("Testing:")
Print TestFuncExample 5: Array parameters
Function x(b() As Double) As Integer
x = UBound(b)-LBound(b)+1
End Function
Dim a(1 To 10) As Double
Print x(a())Dialect Differences
- In
-lang fb,ByValis the default for built-in types (exceptString);Stringand UDTs default toByRef. - In
-lang qband-lang fblite,ByRefis the default. - In
-lang qb, must use assignment to function name to specify return value;Function = ...is not allowed.
Differences from QB
- Parameters can be optional in FreeBASIC.
- Return type can be specified with
As TYPE. Returnstatement can specify return value.- FreeBASIC supports function overloading.
- Function return values can be ignored.