#include "MusimatTutorial.h"Go to the source code of this file.
Functions | |
| MusimatTutorialSection (B0119) | |
| Integer | euclid (Integer m, Integer n) |
| Void | testEuclid () |
| Integer euclid | ( | Integer | m, | |
| Integer | n | |||
| ) |
| MusimatTutorialSection | ( | B0119 | ) |
Definition at line 2 of file B0119.cpp.
References testEuclid().
00002 { 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 }
| Void testEuclid | ( | ) |
Definition at line 28 of file B0119.cpp.
00028 { 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 }}
1.4.7