00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0120) { 00003 extern Integer euclid (Integer m, Integer n); 00004 Print("*** B.1.20 Invoking Functions ***"); 00005 /***************************************************************************** 00006 00007 B.1.20 Invoking Functions 00008 00009 We had two situations where the function euclid() was followed by a list of arguments, once 00010 where it was defined, and another where it was invoked. The arguments associated with the def- 00011 inition of euclid() are called its formal arguments. They are Integer m and Integer n. 00012 The values associated with its invocation (integers 91 and 416) are called its actual arguments. 00013 A function will have only one set of formal arguments that appear where the function is defined. 00014 It will have as many sets of actual arguments as there are invocations of the function in a program. 00015 00016 When a function is invoked, three things happen: 00017 00018 1. The values of the actual arguments are copied to the corresponding formal arguments. 00019 00020 If an actual argument is a constant, its value is simply copied to the corresponding formal argu- 00021 ment. Example: Print(3) copies 3 to the formal argument for Print(). 00022 00023 If an actual argument is a variable, its value is copied to the corresponding formal argument. Exam- 00024 ple: Integer a = 3; Print(a) copies the value of a (which is 3) to the formal argument for 00025 Print(). 00026 00027 If an actual argument is another function, that function is evaluated first, and its return value 00028 replaces the function. Example: For the statement Print(euclid(91, 416)), first 00029 euclid(91, 416) is evaluated, and the result (which is 13) is substituted in its place. So the 00030 statement becomes Print(13). Finally, the 13 is copied to the corresponding formal argument 00031 of the Print() function. 00032 00033 2. The body of the function is executed using the values copied to the formal arguments in the 00034 first step. 00035 00036 3. The return value of the function is substituted for the function call in the enclosing program. 00037 00038 Here's an example. 00039 *****************************************************************************/ 00040 00041 // The following two statements ... 00042 Integer x = euclid(91, 416); 00043 Print( x ); 00044 00045 // ... are equivalent to this one statement: 00046 Print(euclid(91, 416)); 00047 } 00048 00050 /* $Revision: 1.2 $ $Date: 2006/09/05 06:32:25 $ $Author: dgl $ $Name: $ $Id: B0120.cpp,v 1.2 2006/09/05 06:32:25 dgl Exp $ */ 00051 // The Musimat Tutorial � 2006 Gareth Loy 00052 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 00053 // and published exclusively by The MIT Press. 00054 // This program is released WITHOUT ANY WARRANTY; without even the implied 00055 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00056 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00057 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00058 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00059 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00060 // The Musimat website: http://www.musimat.com/ 00061 // This program is released under the terms of the GNU General Public License 00062 // available here: http://www.gnu.org/licenses/gpl.txt 00063