A procedure or function in Darwin is defined with the syntactic construct "proc" ... "end". Functions (returning a value(s)), procedures (functions not returning any value) and Object constructors (functions which return a data structure), are defined by the same construct. A "proc" is the main way of defining procedures, but it is also possible to generate procedures with the arrow notation ("->") and with the use of high level functions, like Inherit, (see objectorientation). Procs are also the main vehicle to define classes or data structures.
A procedure has the following components:
proc( param1:type1, param2:type2, ... ) -> ReturnType;
local var1, var2, .... ;
global gvar1, .... ;
option opt1, .... ;
description '....';
. . . <Body Statement Sequence> . . .
end:
The formal parameters, enclosed by parenthesis right after the "proc" token, define the arguments which may be passed to the procedure. The actual number of arguments passed to the procedure in a call may differ from the number specified in the proc. The following rules apply:
| (1) | The formal parameters have an optional type specification. |
| (2) | All the parameters which have a type, if they are present when calling the procedure, they will by typed-checked. If their type does not match, a suitable error is produced. |
| (3) | Parameters which are not present, are obviously not type-checked. If a non-passed parameter is used, then a suitable error is produced. |
| (4) | Parameters are passed by value/reference. However, if the value of a parameter is just a name, the procedure may further evaluate this name or assign values to it. Data structures or lists can be modified, and if passed as parameters and modified, will remain modified for the caller. |
| (5) | If additional parameters are passed, then this parameters are not checked nor are accessible by name. They can be accessed with "args" (all the parameters) or with "args[i]". |
| (6) | Passing more or less parameters, does not cause an error by itself. Only when a missing parameter is used it will cause an error. The number of parameters which are actually passed, is available in the body of the procedure with the name "nargs". |
| (7) | When defining a class, the parameter names become the field names (and their types) of the class. |
| (8) | Optional parameters are defined in a slightly different way and are separated from the rest by a semicolon ";". For a full description of optional parameters see |
| (9) | OptionalParameters |
| (10) | . |
The type following the arrow ("->") is optional and it indicates the type of result that should be returned. If a type is specified, the procedure will check that the returned value is of this type. If the type does not match, a suitable error is produced. This allows to write procedures which are completely type-safe. If the procedure returns an expression sequence, type checking is not possible.
To pretty-print an entire procedure, i.e. print all the statements reformatted according to darwin's standard indentation rules, you should use print( disassemble(xxx) ) where xxx is a procedure. If xxx is the name of a procedure, then print( disassemble(op(xxx)) ) should be used.