Skip to content

Functions

Rui Ventura edited this page Apr 1, 2017 · 1 revision

A function allows the grouping of a set of statements within its body, basing its execution on a set of parameters (formal arguments), when invoked from an expression.

Declaration

Functions are always designated by constant identifiers preceded by the data type of the value returned by the function. If the function doesn't return a value, the reserved keyword procedure is used instead.

Functions that receive arguments must state them in their header. Functions that don't define an empty header. Export/import qualifier public can't be applied to functions nor can use be applied to the declaration of a function's arguments (see global symbols).

The declaration of a function without a body is used to characterize an external identifier or to perform anticipated declarations (used to pre-declare functions that may be used before being defined, for example, between two mutually recursive functions). In the case that the declaration has a body, a new function is defined (in this case, the use qualifier can't be used).

Invocation

A function can only be invoked through an identifier that's a reference to one that was previously declared or defined.

If there are arguments, in the function invocation, the identifier is followed by a list of expressions delimited by parenthesis. This list is a sequence, possibly empty, of comma-separated expressions. The number and type of the arguments must match the number and type of the invocated function's parameters.

According to the Cdecl convention, the calling function places the arguments on the stack and is responsible for their removal, after returning from the call. Therefore, the arguments must be pushed in the inverse order of their declaration (i.e., they're evaluated from right to left before the function's invocation and the result is passed by value). The return address is pushed on top of the stack by the function call.

Body

A function's body consists of a block that contains (optional) declarations followed by (optional) statements. Neither export (public) or import (use) qualifiers (see global symbols) are applicable inside of a function's body.

The value returned by a function, through the assignment of the special left-value with the function's name, must share the same type as the function's declared return type.

If there is a default return value for the function's return (indicated by the = notation following the function's signature), then it should be used if no other is specified. The specification of the default return value must be a literal of the indicated return type. Specifying a return value if the function is declared as not returning a value is an error (using procedure). A function with a return type of integer or pointer returns 0 (zero) or null by default (i.e., if a return value isn't specified). In every other case, the return value is indefinite if not defined explicitly.

The return statement causes the interruption of a function's flow and its current value is returned to the caller.

Any block (used, for example, in a conditional or iteration statement) can define variables.

Main function and program execution

A program starts by invoking the xpl function (with no arguments). The arguments with which the program was called can be obtained through the functions int argc() (returns the argument count); string argv(int n) (returns the nth argument as a string) (n > 0); and string envp(int n) (returns the nth environment variable as a string) (n > 0).

The main function's return value is returned to the environment that invoked the program. This return value adheres to the following rules (operating system): 0 (zero), successful run; 1 (one), invalid arguments (in number of value); 2 (two), runtime error. Values above 128 indicate the program terminated due to a signal. In general, to operate correctly, programs should return 0 (zero) if the execution was a success and a value other than 0 (zero) in case of an error.

The runtime library (RTS) has information about other available support functions, including system calls.

Clone this wiki locally