FreeBasic Function Keyword
Last modified October 18, 2026
The FreeBasic Function keyword defines a block of code that
performs a specific task and returns a value. Functions help organize code
into reusable units and improve program structure.
Basic Definition
In FreeBasic, a Function is a named procedure that returns
a value. Functions can accept parameters, perform operations, and return
a result to the caller.
Functions promote code reuse and modularity. They encapsulate logic, making programs easier to understand and maintain. FreeBasic functions can return scalar types, user-defined types, and pointers, but not arrays.
Simple Function Example
This example shows a basic function that adds two numbers.
Function AddNumbers(a As Integer, b As Integer) As Integer
Return a + b
End Function
Dim result As Integer = AddNumbers(5, 7)
Print "The sum is: "; result
Here we define an AddNumbers function that takes two integer
parameters. It returns their sum using the Return statement.
We call the function and store its result in a variable.
Function with Multiple Return Statements
Functions can have multiple return points based on conditions.
Function GetMax(a As Integer, b As Integer) As Integer
If a > b Then
Return a
Else
Return b
End If
End Function
Print "Max of 10 and 20: "; GetMax(10, 20)
Print "Max of -5 and 5: "; GetMax(-5, 5)
The GetMax function returns the larger of two numbers. It
demonstrates conditional returns. Only one return statement executes per
function call.
Recursive Function
Functions can call themselves, enabling recursive solutions.
Function Factorial(n As Integer) As Integer
If n <= 1 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function
Print "5! = "; Factorial(5)
Print "7! = "; Factorial(7)
This recursive function calculates factorials. It calls itself with a smaller value until reaching the base case. Recursion is powerful but requires proper termination conditions.
Function Returning Array
Functions can return arrays, allowing complex data structures.
Sub CreateFibonacci(fib() As Integer, size As Integer)
fib(0) = 0
fib(1) = 1
For i As Integer = 2 To size - 1
fib(i) = fib(i - 1) + fib(i - 2)
Next
End Sub
Dim sequence(0 To 9) As Integer
CreateFibonacci(sequence(), 10)
For i As Integer = 0 To 9
Print sequence(i); " ";
Next
Print
FreeBasic functions cannot return an array, so the Fibonacci sequence is
filled by a Sub that receives the array by reference. The
caller passes the array with empty parentheses, sequence().
The output is 0 1 1 2 3 5 8 13 21 34.
Function with Optional Parameters
FreeBasic supports optional parameters with default values.
Declare Function Greet Overload (person As String) As String
Declare Function Greet Overload (person As String, title As String) As String
Function Greet (person As String) As String
Return "Hello, Mr. " & person & "!"
End Function
Function Greet (person As String, title As String) As String
Return "Hello, " & title & " " & person & "!"
End Function
Print Greet("Smith")
Print Greet("Johnson", "Dr.")
FreeBasic does not support default parameter values in the -lang fb
dialect (the Optional keyword with a default value is only accepted
in the deprecated, fblite and qb dialects).
The same effect is achieved with overloaded functions: one version omits the
optional argument and supplies its value internally. The Overload
keyword is required when declaring the overloads.
Function Overloading
FreeBasic allows multiple functions with the same name but different parameters.
Declare Function Add Overload (a As Integer, b As Integer) As Integer
Declare Function Add Overload (a As Single, b As Single) As Single
Function Add (a As Integer, b As Integer) As Integer
Return a + b
End Function
Function Add (a As Single, b As Single) As Single
Return a + b
End Function
Print "Integer add: "; Add(5, 3)
Print "Single add: "; Add(2.5!, 3.7!)
Here we define two Add functions for different types. The
compiler selects the appropriate version based on argument types. This
is called function overloading. The Overload keyword is
required on the declarations of overloaded procedures. Its output is
Integer add: 8 and Single add: 6.2.
Function as Parameter
Functions can accept other functions as parameters.
Function Square(n As Integer) As Integer
Return n * n
End Function
Function Cube(n As Integer) As Integer
Return n * n * n
End Function
Type FnPtr As Function (As Integer) As Integer
Function ApplyOperation(n As Integer, op As FnPtr) As Integer
Return op(n)
End Function
Print "Square of 5: "; ApplyOperation(5, @Square)
Print "Cube of 3: "; ApplyOperation(3, @Cube)
This demonstrates higher-order functions. ApplyOperation takes
a number and a function to apply. The function pointer type is declared with
Type FnPtr As Function (As Integer) As Integer, since an inline
function-pointer type is not accepted as a parameter type. The @
operator gets the function's address. This enables powerful callback patterns.
Best Practices
- Naming: Use verbs or verb phrases that describe the function's action.
- Size: Keep functions small and focused on a single task.
- Parameters: Limit the number of parameters for better readability.
- Documentation: Comment complex functions explaining their purpose.
- Return: Have a single return point when possible for clarity.
This tutorial covered the FreeBasic Function keyword with practical
examples showing various function capabilities and patterns.
Author
List all FreeBasic Tutorials.