ZetCode

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_simple.bas
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_multireturn.bas
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_recursive.bas
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.

function_returnarray.bas
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.

function_optional.bas
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.

function_overload.bas
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_asparam.bas
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

This tutorial covered the FreeBasic Function keyword with practical examples showing various function capabilities and patterns.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.