next up previous
Next: 10. Modules Up: Report on the Programming Previous: 8. Statements

Subsections

  
9. Function declarations

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 .

  
9.1 Formal parameters

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:

1.
the same variable as another actual parameter (of any mode), or
2.
a component of another actual parameter (of any mode), or
3.
a variable of which another actual parameter (of any mode) is a component.

(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:

1.
Variables passed as shared parameters can be read and written.
2.
Variables passed as input parameter are read when the function is called, but cannot be written.
3.
Variables passed as output parameters can be written (but not read).

Other variables cannot be accessed.

  
9.2 Standard functions

Orca supports the following Modula-2 standard functions: ABS, CAP, CHR, FLOAT, MAX, MIN, ODD, ORD, TRUNC, and VAL.

ABS(expr)
Returns the absolute value of the argument.
CAP(ch)
Returns the corresponding upper case letter if ch is a lower case letter; else it returns ch.
CHR(x)
Returns the character with ordinal number x.
FLOAT(expr)
Returns the value of the expression as a real; the expression itself must have the integer type.
MAX(T)
Returns the maximum value of type T. T must be a scalar type.
MIN(T)
Returns the minimum value of type T. T must be a scalar type.
ODD(expr)
Returns 1 if the expression is odd and 0 if it is even.
ORD(expr)
Returns the ordinal value of the expression, which must be of type char or integer or of some enumeration type.
TRUNC(expr)
Returns the value of the expression truncated to an integer.
VAL(T, expr)
Returns the value of type T whose ordinal number is given by the expression.

In addition, Orca provides the following functions:

NCPUS()
Returns the number of processors available to this program. This value remains the same during the entire execution of the program.
MYCPU()
Returns the number of the processor the calling process executes on. Processors are numbered sequentially, the first (initial) processor having number 0.
ADDNODE(g)
Adds a new node to graph g and returns the nodename of it; if g is of type T, the value returned is of type NODENAME OF T. A node can be deleted from a graph through the DELETENODE statement.
DELETENODE(g, p)
Deletes the node named by p from g. A run-time error occurs if g does not have a node with the given name. Also, any future references to the deleted node will cause a run-time error.
LB(expr, d), LB(expr)
Returns the current lower bound of the array expr. d must be a constant integer indicating the dimension of which the lower bound is requested. It may be omitted, in which case it defaults to 1.
UB(expr, d), UB(expr)
Returns the current upper bound of the array expr. d must be a constant integer indicating the dimension of which the upper bound is requested. It may be omitted, in which case it defaults to 1.
SIZE(expr)
Returns the number of elements of the set, bag or array expr.
DELETE(e, desig)
Deletes an element e from a set or bag desig; it has no effect if the element is not a member of the set or bag.
INSERT(e, desig)
Adds a new element e to a set or bag desig.
ASSERT(expr)
Checks if the boolean expression expr yields true; if not, it causes a run-time error.

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.


next up previous
Next: 10. Modules Up: Report on the Programming Previous: 8. Statements
Ceriel JH Jacobs
1999-04-22