Next: Why Use Functions?
Up: Functions
Previous: User Defined Functions

How Functions Work

It is helpful at this point to understand what happens when a function is called. Follow along in the example program as we explain what happens.

When the main program attempts to evaluate LOG2(X), it stops what it is doing and ``calls'' LOG2. A (the formal parameter of LOG2) takes on the value of X (the actual parameter in the call to LOG2), and then the statements in the body of LOG2 are evaluated. When the RETURN statement is encountered, the evaluation of LOG2 terminates and the evaluation of the main program picks up at the point of the call.

As this happens, the value of LOG2(X) is ``returned.'' The value that is returned is the value that was assigned to the special variable LOG2 while the function LOG2 was executing.

Try tracing the execution through on paper, keeping track of what variables take on which values. If you don't understand what's going on, please ask a TA for help.


Next: Why Use Functions?
Up: Functions
Previous: User Defined Functions

Hamlet Project
hamlet@cs.utah.edu