StatementsTopVariable DeclarationsFunctions

Functions

< function definition list > -> < function definition list > < function definition >
|
< function definition > -> void < identifier > < formals part > {
< body >
}
| int < identifier > < formals part > {
< body >
} ;
< formals part > -> ( < formals list > )
| ( )
< formals list > -> < formals list >, < formal declaration>
| <formal declaration>
< formal declaration > -> < type name > * < identifier >
| integer < identifier >

Functions may be defined in any < body > within a program. Functions may only return integer values as results. Functions declared to return void are just procedures.

The scope of the name associated with a function is the entire < body > in which the function's definition occurs. In particular, there is no restriction against forward references to functions. Functions may be called recursively.

Parameters may be passed to a function using either call-by-value or call-by-reference. The inclusion of a * 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 function. This implies that the formal parameter names of a function must be distinct from the names of variables and functions declared within its body.


Computer Science 434
Department of Computer Science
Williams College

StatementsTopVariable DeclarationsFunctions