ZetCode

FreeBasic Return Keyword

last modified June 16, 2025

The FreeBasic Return keyword is used to exit a function or subroutine and optionally return a value. It plays a crucial role in controlling program flow and passing data between procedures.

Basic Definition

In FreeBasic, Return is a statement that immediately exits the current function or subroutine. In functions, it can return a value to the caller. In subroutines, it simply exits without returning a value.

The Return statement can appear anywhere in a procedure. When executed, it transfers control back to the calling code. Multiple return points are allowed but should be used judiciously for code clarity.

Simple Function Return

This example shows a basic function using the Return statement.

simple_return.bas
Function AddNumbers(a As Integer, b As Integer) As Integer
    Dim result As Integer = a + b
    Return result
End Function

Dim sum As Integer = AddNumbers(5, 7)
Print "The sum is: "; sum

The AddNumbers function takes two integers, adds them, and returns the result. The Return statement passes the value back to the caller. This is the most common use of Return in functions.

Early Return from Function

Return can be used to exit a function before reaching its end.

early_return.bas
Function IsPositive(n As Integer) As Boolean
    If n > 0 Then
        Return True
    End If
    
    Return False
End Function

Print "Is 10 positive? "; IsPositive(10)
Print "Is -5 positive? "; IsPositive(-5)

This function checks if a number is positive. It uses an early return when the condition is met. The second return handles the false case. This pattern is common in validation functions.

Return from Subroutine

Subroutines can use Return to exit early, though they don't return values.

subroutine_return.bas
Sub PrintIfPositive(n As Integer)
    If n <= 0 Then
        Print "Number is not positive"
        Return
    End If
    
    Print "The positive number is: "; n
End Sub

PrintIfPositive(8)
PrintIfPositive(-3)

This subroutine prints a number only if it's positive. The Return statement exits early for non-positive numbers. Subroutines use Return for control flow without returning values.

Returning Multiple Values

FreeBasic allows returning multiple values using user-defined types.

multiple_return.bas
Type Point
    x As Integer
    y As Integer
End Type

Function CreatePoint(x As Integer, y As Integer) As Point
    Dim p As Point
    p.x = x
    p.y = y
    Return p
End Function

Dim origin As Point = CreatePoint(0, 0)
Print "Point coordinates: "; origin.x; ", "; origin.y

This example demonstrates returning a compound type. The function creates and returns a Point structure. Return can handle complex types just like simple values.

Return in Recursive Functions

Return is essential for recursive functions to unwind the call stack.

recursive_return.bas
Function Factorial(n As Integer) As Integer
    If n <= 1 Then
        Return 1
    End If
    
    Return n * Factorial(n - 1)
End Function

Print "5! = "; Factorial(5)

This recursive factorial function uses Return for both the base case and recursive case. Each return passes a value back up the call stack. Recursion demonstrates Return's role in nested function calls.

Return with Reference Parameters

Functions can modify reference parameters before returning.

reference_return.bas
Function ProcessString(ByRef s As String) As Boolean
    If Len(s) = 0 Then
        Return False
    End If
    
    s = UCase(s)
    Return True
End Function

Dim text As String = "hello"
If ProcessString(text) Then
    Print "Processed string: "; text
Else
    Print "Empty string provided"
End If

This function modifies a string by reference and returns a status. The Return statement provides feedback about the operation's success. This pattern combines output parameters with return values.

Return in Main Function

The main program can use Return to specify an exit code.

main_return.bas
Function Main() As Integer
    Print "Program starting..."
    
    ' Program logic here
    
    Print "Program ending normally"
    Return 0
End Function

In FreeBasic, the main function can return an integer exit code. Return 0 typically indicates success. Non-zero values often represent error codes. This is useful for scripting and batch processing.

Best Practices

This tutorial covered the FreeBasic Return keyword with practical examples showing its various uses in functions and subroutines.

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.