FreeBasic Declare Keyword
last modified June 16, 2025
The FreeBasic Declare keyword is used for function and procedure
declarations. It allows forward declarations and specifying calling conventions.
Declare is essential for organizing code and working with external libraries.
Basic Definition
In FreeBasic, Declare creates a function or procedure prototype.
It tells the compiler about a function's existence before its implementation.
This enables calling functions before their actual definition in the code.
The Declare statement includes the function name, parameters, return type, and optionally the calling convention. It's particularly useful for large projects and when working with DLLs.
Basic Function Declaration
This example shows a simple function declaration and implementation.
Declare Function AddNumbers(a As Integer, b As Integer) As Integer
Print "Sum: "; AddNumbers(5, 7)
Function AddNumbers(a As Integer, b As Integer) As Integer
Return a + b
End Function
Here we declare AddNumbers before using it. The declaration
specifies the parameter types and return type. The actual implementation
comes later. This allows calling the function before its definition.
Forward Declaration
Forward declarations enable mutual recursion between functions.
Declare Function IsEven(n As Integer) As Boolean
Declare Function IsOdd(n As Integer) As Boolean
Print "5 is odd: "; IsOdd(5)
Print "4 is even: "; IsEven(4)
Function IsEven(n As Integer) As Boolean
If n = 0 Then Return True
Return IsOdd(n - 1)
End Function
Function IsOdd(n As Integer) As Boolean
If n = 0 Then Return False
Return IsEven(n - 1)
End Function
This example demonstrates mutual recursion between IsEven and
IsOdd. Forward declarations are required because each function
calls the other. Without them, the compiler wouldn't recognize the functions.
Procedure Declaration
Procedures (subroutines without return values) can also be declared.
Declare Sub PrintMessage(msg As String)
PrintMessage("Hello from FreeBasic!")
Sub PrintMessage(msg As String)
Print "Message: "; msg
End Sub
This shows a simple procedure declaration. The Sub keyword
indicates no return value. The declaration allows calling the procedure
before its implementation appears in the code.
External Function Declaration
Declare is essential for using functions from external libraries.
Declare Function MessageBox Lib "user32.dll" Alias "MessageBoxA" _
(ByVal hWnd As Integer, ByVal lpText As String, _
ByVal lpCaption As String, ByVal uType As Integer) As Integer
MessageBox(0, "Hello from FreeBasic!", "Message", 0)
This declares the Windows API MessageBox function. The Lib
clause specifies the DLL. Alias maps to the actual function name.
Parameters and return type match the Windows API specification.
Calling Convention
Declare can specify different calling conventions for functions.
Declare Function CDeclFunction CDecl(n As Integer) As Integer
Declare Function StdCallFunction StdCall(n As Integer) As Integer
Print "CDecl result: "; CDeclFunction(5)
Print "StdCall result: "; StdCallFunction(5)
Function CDeclFunction CDecl(n As Integer) As Integer
Return n * 2
End Function
Function StdCallFunction StdCall(n As Integer) As Integer
Return n * 3
End Function
This demonstrates specifying calling conventions. CDecl and
StdCall are common conventions used in different APIs. The
declaration must match the implementation's calling convention.
Array Parameters
Declare can specify array parameters in function declarations.
Declare Function SumArray(arr() As Integer, size As Integer) As Integer
Dim numbers(4) As Integer = {1, 2, 3, 4, 5}
Print "Array sum: "; SumArray(numbers(), 5)
Function SumArray(arr() As Integer, size As Integer) As Integer
Dim total As Integer = 0
For i As Integer = 0 To size - 1
total += arr(i)
Next
Return total
End Function
This shows declaring a function that takes an array parameter. The empty parentheses indicate an array. The size parameter tells the function how many elements to process. Array parameters are always passed by reference.
Optional Parameters
Declare can specify optional parameters with default values.
Declare Function Greet(name As String, Optional title As String = "Mr.") As String
Print Greet("Smith")
Print Greet("Johnson", "Dr.")
Function Greet(name As String, Optional title As String = "Mr.") As String
Return "Hello, " + title + " " + name + "!"
End Function
This demonstrates optional parameters in declarations. The Optional
keyword marks parameters that can be omitted. Default values are specified
in the declaration. Callers can provide fewer arguments than parameters.
Best Practices
- Consistency: Keep declarations and implementations synchronized.
- Documentation: Document parameters and return values.
- Organization: Group related declarations together.
- External Functions: Verify calling conventions.
- Forward Declarations: Use when mutual recursion is needed.
This tutorial covered the FreeBasic Declare keyword with practical
examples showing its various uses in function and procedure declarations.
Author
List all FreeBasic Tutorials.