Procedures
Defining and calling
Procedure.i Add(a.i, b.i)
ProcedureReturn a + b
EndProcedure
OpenConsole()
PrintN(Str(Add(2, 3)))
Output:
5
The tag after Procedure is the return type. Without one, a procedure returns int, and returns 0 if it ends without ProcedureReturn.
The same procedure in C style:
int Add(int a, int b) {
return a + b
}
Write void as the C-style return type for a procedure that returns nothing.
Declare before use
A procedure must be defined, or declared, before the code that calls it. To call one that is defined further down, Declare it first:
Declare.i Twice(n.i)
OpenConsole()
PrintN(Str(Twice(21)))
Procedure.i Twice(n.i)
ProcedureReturn n * 2
EndProcedure
Output:
42
This rule is what lets the compiler tell a call name(…) from an array element name(…) as soon as it reads it.
A Declare must come before the procedure it declares. One that comes after it does nothing, and x warns about it (PureBasic stops with an error).
Parameters
Parameters are local variables that start with the values passed. Numbers and pointers are passed by value.
Default values
Parameters with a default can be left out by the caller. They must come last.
Procedure.s Greet(name$, greeting$ = "Hello")
ProcedureReturn greeting$ + ", " + name$
EndProcedure
OpenConsole()
PrintN(Greet("Ada"))
PrintN(Greet("Grace", "Hi"))
Output:
Hello, Ada
Hi, Grace
Strings
Strings are passed by value: the procedure gets its own copy to change. Thanks to reference counting, no characters are copied unless the procedure changes the string. See Strings.
Arrays
Array name.type(dimensions) passes an array by reference. Changes made by the procedure are seen by the caller.
Procedure Fill(Array values.i(1), v.i)
For i = 0 To ArraySize(values())
values(i) = v
Next
EndProcedure
OpenConsole()
Dim a.i(3)
Fill(a(), 7)
PrintN(Str(a(0) + a(3)))
Output:
14
The number in brackets is the number of dimensions, not a size.
🔜 List and Map parameters.
Local variables
Variables first used inside a procedure, and those declared with Protected, belong to that procedure and start at 0 on every call. Static variables keep their value between calls. See Types and variables.
Recursion
Procedures can call themselves:
Procedure.q Factorial(n.q)
If n <= 1
ProcedureReturn 1
EndIf
ProcedureReturn n * Factorial(n - 1)
EndProcedure
OpenConsole()
PrintN(Str(Factorial(20)))
Output:
2432902008176640000
Unused procedures
Procedures that are never called are left out of the executable.
Calling conventions
Procedures use the platform's standard calling convention (Microsoft x64 on Windows), so their addresses can be given to the operating system as callbacks.
🔜 ProcedureC and ProcedureDLL as separate conventions (on 64-bit Windows there is only one).
Procedure pointers: Prototype ✅
@Name() is the address of a procedure. To call a procedure through its address, declare the kind of procedure with Prototype, written like Declare. Then a variable, parameter or structure field of that type holds an address and is called like a procedure:
Prototype.i BinOp(a.i, b.i)
Procedure.i Add(a.i, b.i)
ProcedureReturn a + b
EndProcedure
Procedure.i Mul(a.i, b.i)
ProcedureReturn a * b
EndProcedure
Procedure.i Apply(f.BinOp, a.i, b.i)
ProcedureReturn f(a, b)
EndProcedure
OpenConsole()
Define op.BinOp = @Add()
PrintN(Str(op(2, 3)))
op = @Mul()
PrintN(Str(op(4, 5)) + " " + Str(Apply(@Add(), 7, 8)))
Output:
5
20 15
The arguments are checked against the prototype, as for a normal call. A procedure pointer is an address like any other: it can be compared with 0, stored in arrays and structures, and passed to the operating system as a callback. Calling a pointer that holds 0 crashes, as in PureBasic, so check it first when it may be unset.
PrototypeC is accepted and means the same on 64-bit Windows. Through a prototype, string parameters follow x's rules for its own procedures. To call a DLL function found at run time that takes a string, give the prototype a pointer parameter (*text) and pass @text$.