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