00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0119) { 00003 Print("*** B.1.19 User-Defined Functions ***"); 00004 /***************************************************************************** 00005 00006 B.1.19 User-Defined Functions 00007 00008 Musimat, like most programming languages, allows users to define their own functions. Take 00009 Euclid's method, for example. To define it, we must state how the input variables m and n receive 00010 their inputs, and determine what happens to the result when the method halts. We can define a func- 00011 tion named euclid() in Musimat as follows: 00012 *****************************************************************************/ 00013 testEuclid(); // The tutorial continues in this function (step into the function). 00014 } 00015 00016 Integer euclid (Integer m, Integer n){ 00017 Repeat { 00018 Integer r = Mod(m, n); 00019 If (r == 0) 00020 Return(n); 00021 Else { 00022 m = n; 00023 n = r; 00024 } 00025 } 00026 } 00027 00028 Void testEuclid() { 00029 /***************************************************************************** 00030 The function euclid() is declared to be of type Integer because it will return an Integer result. 00031 00032 Note that Return(n) has been substituted for the Halt(n) function shown previously. Instead 00033 of halting execution altogether, the Return(n) statement only exits the current function, carrying 00034 with it the value of its argument back to the context that invoked it. The program can then continue 00035 executing from there, if there are statements following its invocation. Here's an example of invok- 00036 ing the euclid() function: 00037 *****************************************************************************/ 00038 00039 Integer x = euclid(91, 416); 00040 Print(x); 00041 00042 /***************************************************************************** 00043 which will print 13. If we had used Halt() in euclid(), we'd never reach the Print statement 00044 because the program would stop. 00045 00046 Here's another way to compute the same thing: 00047 *****************************************************************************/ 00048 00049 Print(euclid(91, 416)); 00050 00051 /***************************************************************************** 00052 This way we can eliminate the "middleman" variable x, which only existed to carry the value from 00053 the euclid() function to the Print() function. In this example, the call to the euclid() func- 00054 tion is nested within the Print() function. Musimat invokes the nested function first, and the 00055 value that euclid() returns is supplied automatically as an argument to the enclosing function, 00056 Print(). Functions can be nested to an arbitrary extent. The most deeply nested function is 00057 always called first. 00058 00059 *****************************************************************************/ 00060 } 00061 00063 /* $Revision: 1.3 $ $Date: 2006/09/05 08:03:08 $ $Author: dgl $ $Name: $ $Id: B0119.cpp,v 1.3 2006/09/05 08:03:08 dgl Exp $ */ 00064 // The Musimat Tutorial © 2006 Gareth Loy 00065 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00066 // and published exclusively by The MIT Press. 00067 // This program is released WITHOUT ANY WARRANTY; without even the implied 00068 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00069 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00070 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00071 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00072 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00073 // The Musimat website: http://www.musimat.com/ 00074 // This program is released under the terms of the GNU General Public License 00075 // available here: http://www.gnu.org/licenses/gpl.txt 00076