ZetCode

FreeBasic Function Keyword

last modified June 16, 2025

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 any data type.

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
Function CreateFibonacci(size As Integer) As Integer()
    Dim fib(size - 1) 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
    
    Return fib
End Function

Dim sequence() As Integer = CreateFibonacci(10)
For i As Integer = 0 To 9
    Print sequence(i); " ";
Next

This function generates and returns a Fibonacci sequence array. Note the parentheses in the return type declaration. The caller receives a complete array structure.

Function with Optional Parameters

FreeBasic supports optional parameters with default values.

function_optional.bas
Function Greet(name As String, Optional title As String = "Mr.") As String
    Return "Hello, " & title & " " & name & "!"
End Function

Print Greet("Smith")
Print Greet("Johnson", "Dr.")

The Greet function has a required and an optional parameter. When omitted, the optional parameter uses its default value. Optional parameters must come after required ones.

Function Overloading

FreeBasic allows multiple functions with the same name but different parameters.

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

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

Function ApplyOperation(n As Integer, op As Function (As Integer) As Integer) 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 @ 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.