-
Notifications
You must be signed in to change notification settings - Fork 5
Functions
To declare a function, you must specify the type and name of a function. For the function to be defined, the body must be specified after the name of the function.
The body of the function may either be a compound body, declared using a pair of brackets, or an expression-body, which is declared using an equals. Examples:
// compound body
func @test() -> i32 {
return 5;
}
// expression-body
func @test -> i32 = 5;
When using an expression-body, the body must only be a single statement or expression, and have the same type as that of the function return type.
A function will automatically create it's own scope, which means that any argument or variable inside the function may only be accessed inside of that function.
Functions must return a value unless they have type void
, in which case they will assume to return a void value at the end of the function.
Functions may be declared with parameters, which go inside of the parentheses in the declaration like so:
func @square(int32 num) -> i32 {
return num * num;
}
A function may also be a member function definition. These belong to a specific struct type and have an implicit this
parameter which is a pointer to the instance of the struct type that it has been called on. Member functions must be defined inside their owning struct.
To call a function, simply type the name of the function followed by a set of parentheses containing any arguments you would like to pass to the function:
MyFunc()
Square(24)
If you wish to call an member function, use the member access operator like so:
test.add(2)
test.getID()
The arguments you pass to the function will follow the C Calling Convention of the platform.
The arguments you pass to the function must match the types of the parameters in the function declaration. A function call may be an expression or a statement.
Wiki created by @solar-mist and @Kami