CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

Procedures and Functions

1. Procedures with more than one parameter.
All formal parameters are listed in the parentheses following the procedure name. There can be as many as you need. When the procedure is called, the actual parameters are matched to the formal parameters in the order they are listed. There must be the same number of actual parameters as there are formal parameters and they must have matching types.

Example:

2. Procedures with local variables.
A procedure may have local variables in addition to the parameters. The local variables are listed just below the procedure header. The variables are local to the procedure, so they cannot be accessed by any other procedure or by the main program.

Example:

3. Functions.
Functions are subprograms that return a value. They behave like procedures in most respects, except they return a value to the calling program. The type of value returned is specified at the end of the function header. The name of the function is used like a variable within the function definition. At the end of the function execution the value of this identifier is returned to the calling program.

Example:

Another Example: