Functions

/Users/garethloy/Musimathics/Musimat1.2/MusimatTutorial/B0120.cpp File Reference

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0120)

Function Documentation

MusimatTutorialSection ( B0120   )

Definition at line 2 of file B0120.cpp.

References euclid(), and x.

                              {
        extern Integer euclid (Integer m, Integer n);
        Print("*** B.1.20 Invoking Functions ***");
        /*****************************************************************************
         
         B.1.20 Invoking Functions
         
         We had two situations where the function euclid() was followed by a list of arguments, once 
         where it was defined, and another where it was invoked. The arguments associated with the def-
         inition of euclid() are called its formal arguments. They are Integer m and Integer n. 
         The values associated with its invocation (integers 91 and 416) are called its actual arguments. 
         A function will have only one set of formal arguments that appear where the function is defined. 
         It will have as many sets of actual arguments as there are invocations of the function in a program.
         
         When a function is invoked, three things happen:
         
         1. The values of the actual arguments are copied to the corresponding formal arguments.
         
         If an actual argument is a constant, its value is simply copied to the corresponding formal argu-
         ment. Example: Print(3) copies 3 to the formal argument for Print().
         
         If an actual argument is a variable, its value is copied to the corresponding formal argument. Exam-
         ple: Integer a = 3; Print(a) copies the value of a (which is 3) to the formal argument for 
         Print().
         
         If an actual argument is another function, that function is evaluated first, and its return value 
         replaces the function. Example: For the statement Print(euclid(91, 416)), first 
         euclid(91, 416) is evaluated, and the result (which is 13) is substituted in its place. So the 
         statement becomes Print(13). Finally, the 13 is copied to the corresponding formal argument 
         of the Print() function.
         
         2. The body of the function is executed using the values copied to the formal arguments in the 
         first step.
         
         3. The return value of the function is substituted for the function call in the enclosing program. 
         
         Here's an example. 
         *****************************************************************************/
        
        // The following two statements ...
        Integer x = euclid(91, 416);
        Print( x );
        
        // ... are equivalent to this one statement:
        Print(euclid(91, 416));
}