Prev Up Next
Go backward to Variable Declarations
Go up to Top
Go forward to Statements

Procedures and Functions

< procedure definition list > -> < procedure definition list > < procedure definition >
|
< procedure definition > -> procedure < identifier > < formals part > ;
< body >
end ;
| function < identifier > < formals part > ;
< body >
end ;
< formals part > -> ( < formals list > )
|
< formals list > -> < formals list >, < formal declaration>
| <formal declaration>
< formal declaration > -> var < identifier > : < type name >
| < identifier > : integer

Procedures and functions may be defined in any < body > within a program. The scope of the name associated with a procedure is the entire < body > in which the procedure's definition occurs. In particular, there is no restriction against forward references to procedures. Procedures and functions may be called recursively.

Parameters may be passed to a procedure using either call-by-value or call-by-reference. The inclusion of the keyword var in a formal declaration indicates that call-by-reference should be used when handling that parameter. All other parameters are passed by value. Only scalar values (integers) can be passed by value.

For the purposes of scoping, formal declarations are treated as if they occurred within the < body > of the procedure. This implies that the formal parameter names of a procedure must be distinct from the names of variables and procedures declared within its body.

Functions may only return values of type integer. Accordingly, no return type specification is required in a function's definition.


Computer Science 434
Department of Computer Science
Williams College

Prev Up Next