Skip to content

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 Function

Parameters

  • identifier — Function name.
  • external_identifier — Externally visible name for linker, enclosed in quotes.
  • parameter_listparameter[, 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:

  1. Return value — immediately exits and returns the value.
  2. Function = value — sets return value but does not exit.
  3. 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

vb
Declare Function ReturnTen () As Integer

Print ReturnTen ()

Function ReturnTen() As Integer
    Return 10
End Function

Example 2: Using assignment to function name

vb
Function ReturnTen() As Integer
    ReturnTen = 10
End Function

Example 3: Function overloading

vb
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 Function

Example 4: Optional parameters

vb
Function TestFunc(P As String = "Default") As String
    Return P
End Function

Print TestFunc("Testing:")
Print TestFunc

Example 5: Array parameters

vb
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, ByVal is the default for built-in types (except String); String and UDTs default to ByRef.
  • In -lang qb and -lang fblite, ByRef is 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.
  • Return statement can specify return value.
  • FreeBASIC supports function overloading.
  • Function return values can be ignored.

See Also

Translated from FreeBASIC official docs. Contact us for removal if infringed.
FreeBASIC is an open-source project, not affiliated with Microsoft