Functions

Functions are reusable blocks of code that can accept parameters and return values.



Declaring Functions

Basic function with no parameters or return value:

Function SayHello()
    Print "Hello, World!"
EndFunction

Calling the function:

SayHello()  ; Output: Hello, World!


Function Parameters

Functions can accept input values called parameters:

Function Greet(name:String)
    Print "Hello, " & name & "!"
EndFunction

Greet("Alice")  ; Output: Hello, Alice!
Greet("Bob")    ; Output: Hello, Bob!

Multiple parameters:

Function Add(a:Int, b:Int)
    Local result:Int = a + b
    Print "Result: " & ToString(result)
EndFunction

Add(5, 3)   ; Output: Result: 8
Add(10, 20) ; Output: Result: 30


Return Values

Functions can return a value using the Return statement:

Function Add:Int(a:Int, b:Int)
    Return a + b
EndFunction

Local sum:Int = Add(5, 3)
Print sum  ; Output: 8

Return type specified after function name:

Function GetName:String()
    Return "BambooBasic"
EndFunction

Function GetPI:Double()
    Return 3.14159
EndFunction

Function IsReady:Int()
    Return 1  ; Use 1 for true, 0 for false
EndFunction


Early Return

Use Return to exit a function early:

Function Divide:Double(a:Double, b:Double)
    If b = 0 Then
        Print "Error: Division by zero"
        Return 0.0
    EndIf
    Return a / b
EndFunction


Local Variables in Functions

Variables declared with Local inside a function are only accessible within that function:

Function Calculate:Int(x:Int)
    Local temp:Int = x * 2
    Local result:Int = temp + 10
    Return result
EndFunction

; temp and result don't exist outside the function


Global Variables

Functions can access and modify global variables:

Global score:Int = 0

Function AddPoints(points:Int)
    score = score + points
    Print "Score: " & ToString(score)
EndFunction

AddPoints(10)  ; Score: 10
AddPoints(5)   ; Score: 15


Function Examples

Calculate factorial:

Function Factorial:Int(n:Int)
    If n <= 1 Then
        Return 1
    EndIf
    Return n * Factorial(n - 1)
EndFunction

Print Factorial(5)  ; Output: 120

Check if number is even:

Function IsEven:Int(n:Int)
    If n Mod 2 = 0 Then
        Return 1  ; True
    Else
        Return 0  ; False
    EndIf
EndFunction

If IsEven(10) Then
    Print "10 is even"
EndIf

String helper function:

Function FormatName:String(firstName:String, lastName:String)
    Return lastName & ", " & firstName
EndFunction

Print FormatName("John", "Smith")  ; Output: Smith, John


Main Function

BambooBasic programs must have a Main function that serves as the application's entry point:

Function Main()
    Print "Program started"

    ; Your code here

    Return 0
EndFunction

The Main function is where your program begins execution.



Examples

See the Functions Example for complete demonstrations.


BambooBasic © 2026 Michael Denathorn