FUNCTION Declaration

Syntax:
FUNCTION name [( [BYVAL|BYREF] param [AS type] [, ...] )] [AS returnType]
    ... statements ...
END FUNCTION

Description

A FUNCTION declares a callable procedure that returns a value to the caller. Functions are the primary building blocks of PBXA64 programs. Each function has a name, optional parameters with optional type annotations, and a return type.

Parameters can be passed BYVAL (by value — the default) or BYREF (by reference — the function receives a pointer to the caller's variable). The return value is set by assigning to the function name within the function body, or by using FUNCTION = expression.

A function terminates when execution reaches END FUNCTION or when EXIT FUNCTION is encountered.

Parameters

ParameterDescription
nameThe identifier name of the function. Must be unique within the program scope. Follows standard PBXA64 naming rules (letter or underscore followed by letters, digits, or underscores).
BYVALOptional keyword. Passes the argument by value (default). The function receives a copy of the value.
BYREFOptional keyword. Passes the argument by reference. The function receives a pointer to the caller's variable; modifications affect the caller's variable.
paramThe parameter name used within the function body.
AS typeOptional type specification for the parameter or return value. Supported types include LONG, DWORD, INTEGER, BYTE, STRING, SINGLE, DOUBLE, QUAD, CUR, EXT, and user-defined TYPEs.
returnTypeOptional. The data type of the function's return value. If omitted, the function returns the default variant type.

Example

FUNCTION Add(BYVAL a AS LONG, BYVAL b AS LONG) AS LONG
    FUNCTION = a + b
END FUNCTION

FUNCTION GetFullName(BYREF first AS STRING, BYREF last AS STRING) AS STRING
    FUNCTION = first + " " + last
END FUNCTION

FUNCTION PBMAIN() AS LONG
    LOCAL sum AS LONG
    sum = Add(10, 20)
    PRINT "Sum: "; sum

    PRINT "Name: "; GetFullName("Ada", "Lovelace")
    FUNCTION = 0
END FUNCTION

Generated Assembly

Function Prologue (Stack Frame Setup)

; Standard Win64 prologue
push   rbp                          ; save old base pointer
mov    rbp, rsp                     ; set new base pointer
sub    rsp, frame_size              ; allocate stack frame for locals

; Save non-volatile registers (if used)
push   rbx
push   rsi
push   rdi
push   r12
push   r13
push   r14
push   r15

; Initialize BYVAL parameters (copy from registers to stack)
mov    [rbp - param1_offset], rcx    ; 1st BYVAL param -> stack
mov    [rbp - param2_offset], rdx    ; 2nd BYVAL param -> stack
mov    [rbp - param3_offset], r8     ; 3rd BYVAL param -> stack
mov    [rbp - param4_offset], r9     ; 4th BYVAL param -> stack
; 5th+ params are already on the caller's stack above RSP

; Initialize BYREF parameters (store pointer directly)
; (No copy needed, pointer is already in RCX/RDX/etc.)

Function Epilogue (Return Value Handling)

; Restore non-volatile registers
pop    r15
pop    r14
pop    r13
pop    r12
pop    rdi
pop    rsi
pop    rbx

; Load return value into RAX (integer) or XMM0 (float)
mov    rax, [rbp - return_offset]    ; integer return
; or
movsd  xmm0, [rbp - return_offset]   ; float return

; Restore stack frame
add    rsp, frame_size
pop    rbp
ret

EXIT FUNCTION

jmp    __pb_func_epilogue           ; jump directly to epilogue

Notes