FreeBasic Overload Keyword
last modified June 16, 2025
The FreeBasic Overload
keyword enables function overloading,
allowing multiple functions with the same name but different parameters.
This provides polymorphism and makes APIs more intuitive to use.
Basic Definition
Function overloading lets you create several functions with identical names but different parameter lists. The compiler selects the appropriate version based on the arguments passed during the function call.
In FreeBasic, overloaded functions are first declared using
Declare Sub/Function Name Overload
, then implemented without the
Overload
keyword. The Overload
keyword in the
declaration tells the compiler that this function name can have multiple
versions. Overloaded functions must differ in parameter types or count.
Return type alone doesn't distinguish overloads.
Simple Integer Overload
This example shows basic function overloading with integer parameters.
Declare Sub Display Overload (n As Integer) Declare Sub Display Overload (n As Double) Sub Display (n As Integer) Print "Integer: "; n End Sub Sub Display (n As Double) Print "Double: "; n End Sub Display(10) Display(3.1415)
We first declare two overloaded Display
functions using
Declare Sub Display Overload
- one for integers and one for
doubles. Then we implement each function without the Overload
keyword, and finally call them. The compiler selects the appropriate version
based on the argument type. This makes the API more intuitive and type-safe.
Different Parameter Counts
Functions can be overloaded by having different numbers of parameters.
Declare Sub Greet Overload () Declare Sub Greet Overload (fname As String) Declare Sub Greet Overload (fname As String, times As Integer) Sub Greet() Print "Hello!" End Sub Sub Greet(fname As String) Print "Hello, "; fname; "!" End Sub Sub Greet(fname As String, times As Integer) For i As Integer = 1 To times Print "Hello, "; fname; "!" Next End Sub Greet() Greet("Alice") Greet("Bob", 3)
Here we first declare three overloaded versions of Greet
with 0, 1,
and 2 parameters using Declare Sub Greet Overload
. Then we implement
each version and call them in the main program. Each version provides different
functionality while maintaining a consistent naming scheme. This demonstrates how
overloading can simplify APIs.
Mixed Parameter Types
Overloaded functions can have completely different parameter types.
Declare Sub Process Overload (value As Integer) Declare Sub Process Overload (text As String) Declare Sub Process Overload (arr() As Integer) Sub Process(value As Integer) Print "Processing integer: "; value * 2 End Sub Sub Process(text As String) Print "Processing string: "; UCase(text) End Sub Sub Process(arr() As Integer) Print "Processing array with "; UBound(arr) - LBound(arr) + 1; " elements" End Sub Process(42) Process("hello") Dim numbers(1 To 5) As Integer Process(numbers())
This example shows three overloaded Process
functions handling different
data types. We first declare each version with Declare Sub Process Overload
,
then implement them, and finally call them. The compiler selects the correct version
based on argument type. This approach keeps function names semantic while handling
diverse inputs.
Overloaded Constructors
Constructors in UDTs (User Defined Types) can also be overloaded. They are implicitly overloaded based on the parameters passed during object creation. This allows for flexible initialization of objects.
In FreeBasic, constructors must be declared inside the Type
block
and defined outside of it.
Type Point x As Integer y As Integer Declare Constructor() Declare Constructor(x As Integer, y As Integer) End Type Constructor Point() x = 0 y = 0 End Constructor Constructor Point(x As Integer, y As Integer) This.x = x This.y = y End Constructor Dim p1 As Point Dim p2 As Point = Point(10, 20) Print "p1: ("; p1.x; ", "; p1.y; ")" Print "p2: ("; p2.x; ", "; p2.y; ")"
The Point
type has two constructors - a default one that
initializes to (0,0) and one that takes explicit coordinates. This provides
flexibility when creating objects while maintaining clean initialization.
No overloading with ByRef and ByVal
It is not possible to overload functions based solely on ByRef and ByVal parameter passing. The compiler does not consider these as distinct overloads.
Sub Modify(n As Integer) n += 10 Print "ByVal modified: "; n End Sub Sub ModifyByRef(ByRef n As Integer) n += 10 Print "ByRef modified: "; n End Sub Dim num As Integer = 5 Modify(num) Print "Original after ByVal: "; num ModifyByRef(num) Print "Original after ByRef: "; num
In the example above, we differentiate between Modify
and
ModifyByRef
by their parameter passing methods. However, this
does not constitute overloading. The compiler treats them as separate
functions due to their different names, not because of the parameter passing
methods.
Overloading with Different Return Types
While return types alone can't overload functions, they can differ when parameters vary.
Declare Function Convert Overload (n As Integer) As String Declare Function Convert Overload (s As String) As Integer Function Convert(n As Integer) As String Return Str(n) End Function Function Convert(s As String) As Integer Return Val(s) End Function Print "String from integer: "; Convert(42) Print "Integer from string: "; Convert("123")
This example shows two overloaded Convert
functions with different
parameter and return types. We first declare each function with
Declare Function Convert Overload
, then implement them, and finally
call them. While the return types differ, the overloading is based on parameter
types. This provides bidirectional conversion in a clean API.
Best Practices
- Consistency: Keep overloaded functions semantically similar.
- Clarity: Avoid overloading with radically different behaviors.
- Documentation: Clearly document all overload variants.
- Default Parameters: Consider using them instead of many overloads.
- Ambiguity: Avoid overloads that could cause confusion.
This tutorial covered the FreeBasic Overload
keyword with practical
examples showing its usage in different scenarios. Function overloading is a
powerful tool for creating clean, intuitive APIs.
Author
List all FreeBasic Tutorials.