A function in Orca is similar to a Modula-2 procedure. Functions may (but need not) return a result, as indicated in the function declaration. If a function returns a result, its body must be exited through a RETURN statement that defines the return value.
A function declaration consists of a specification and an implementation part. A function specification may only appear in a module or object specification (see Sections 10 and 11). Constants, variables, and types declared within a function are not visible outside the function. Functions cannot be nested.
FunctionSpec = FUNCTION ident FormalParams [':' qualident]';' . FunctionImpl = FunctionSpec block . block = {declaration} BEGIN StatementSeq END';' . declaration = ConstantDecl | TypeDecl | VariableDecl .
There are three kinds of formal parameters for functions, operations and processes: input, output, and shared parameters. By default, parameters are input. An operation may only have input and output parameters (see Section 11). A process may only have input parameters and shared parameters of an object type (see Section 13).
A formal input parameter stands for a local variable that is initialized with the value of the actual parameter, which must be an expression of the same type as the formal. The expression is evaluated before the function, operation, or process is activated. If the actual parameter is an array, the formal parameter has the same lower and upper bound(s) as the actual parameter. The formal parameter can be modified, but doing so does not affect the actual parameter.
A formal output parameter is a local variable; it is initialized using the same rules as for variable declarations (see Section 6). (For output parameters of an array or union type, the array bounds or tag may be initialized.) The actual parameter must be a variable or component of the same type. On return of the function or operation, the value of the formal is assigned to the actual parameter. The semantics of this assignment are the same as for normal assignment statements (see Section 8.1). Therefore, output parameters are not allowed for object types or types containing objects types as (sub-) components. Output parameters essentially allow a function or operation to return multiple results.
If a formal parameter is shared, the formal parameter denotes the actual parameter, which must be a variable of the same type. A compile-time or run-time error occurs if one shared or output actual parameter denotes either:
(This rule eliminates the possibility of aliasing.)
FormalParams = '(' [FPSection {';' FPSection} ] ')' . FPSection = IdentList ':' FormalSpec . FormalSpec = [IN | SHARED] FormalType | OUT subtype . FormalType = qualident .
Note that a function can read or modify its environment (i.e., non-local variables) only in the following three ways:
Other variables cannot be accessed.
Orca supports the following Modula-2 standard functions: ABS, CAP, CHR, FLOAT, MAX, MIN, ODD, ORD, TRUNC, and VAL.
In addition, Orca provides the following functions:
Also, Orca supports the pseudo-functions Read, Write and WriteLine. These functions can be called with any number of arguments. Each argument must have one of the following types: integer, real, char, or string. The function Read reads its arguments from the standard input; The function Write prints its arguments on the standard output, in readable (ASCII) form; WriteLine is similar to Write, but in addition prints a new-line symbol after all parameters have been printed.
The functions MYCPU, Read, Write, and WriteLine should not be called from within operations (neither directly nor indirectly through other functions), or the behavior will be undefined.