00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0115) { 00003 Print("*** B.1.15 Functions ***"); 00004 /***************************************************************************** 00005 00006 B.1.15 Functions 00007 00008 Functional notation in mathematics allows us to encapsulate and name arithmetic expressions. For 00009 example, if we have defined the function f (a, b) = a + b, then f stands for a + b. The value or values 00010 in parentheses after a function name, called arguments, supply the function with inputs. Functions 00011 also typically return a result. For example, using this definition of f, 7 = f (3, 4). 00012 00013 Programming languages typically come with a set of predefined functions for the most common 00014 necessities, and they also allow new functions to be created. For example, in Musimat all oper- 00015 ators also have a functional representation, so writing 00016 *****************************************************************************/ 00017 00018 Real x = Divide(11.0, 4.0); 00019 00020 /***************************************************************************** 00021 is the same as writing 00022 *****************************************************************************/ 00023 00024 Real e = 11.0/4.0; 00025 00026 /***************************************************************************** 00027 In this case, e is set to the quotient, 2.75. Real division is performed because both numerator 00028 and denominator are reals. If we want to perform integer division, both numerator and denominator 00029 must be integers. We could write 00030 *****************************************************************************/ 00031 00032 Integer f = Divide(11, 4); 00033 00034 /***************************************************************************** 00035 or equivalently, 00036 *****************************************************************************/ 00037 00038 Integer g = 11/4; 00039 00040 /***************************************************************************** 00041 In either case, f and g are set to the quotient, 2, and the remainder is discarded. To get the remainder after 00042 integer division we can write 00043 *****************************************************************************/ 00044 00045 Integer h = Mod( 11, 4 ); // remainder of integer division 00046 00047 /***************************************************************************** 00048 The variable h is set to 3, the remainder of 11/4. For positive integers m and n, Mod(m, n) 00049 lies between 0 and n - 1. Why is this function called Mod instead of, say, Remainder? See appen- 00050 dix A, A.6. The equivalent operator form for remaindering also looks a little strange: 00051 *****************************************************************************/ 00052 00053 Integer i = 11 % 4; 00054 00055 /***************************************************************************** 00056 The % sign does not have its usual meaning of “percent” in Musimat. Instead, it means “remain- 00057 der of integer division.” Mod and % can only be applied to integer operands. 00058 00059 Some useful built-in functions are not associated with operators. Exponentiation is performed 00060 by the function Pow(). These three statements, 00061 *****************************************************************************/ 00062 00063 Real base = 10.0; 00064 Real exp = 2.0; 00065 Real j = Pow(base, exp); 00066 00067 /***************************************************************************** 00068 are equivalent to writing "j = 10.0 to the power of 2.0", and the result stored in j is 100.0. 00069 Going the other way: Log10(x) is equivalent to "log base 10 of x", and 00070 *****************************************************************************/ 00071 Real k = Log10(100.0); 00072 /***************************************************************************** 00073 sets k to 2. 00074 00075 Another built-in function, Print(), allows us to observe the value of a variable or expression. 00076 When executed, the statements 00077 *****************************************************************************/ 00078 00079 Real l = 11.0/4.0; 00080 Print(l); 00081 00082 /***************************************************************************** 00083 display the value of l, or 2.75. The way in which the value is displayed varies with the type of the 00084 expression and the type of computer. If the computer is a person, for example, he or she might say “two 00085 point seven five.” If it is an electromechanical computer, it might show the value on a display screen. 00086 00087 When the predefined function Halt() is executed, the method in progress stops at that step in 00088 the program. The argument to Halt(), if any, can be used to indicate the answer or result obtained 00089 by the program up to that point. 00090 00091 One final built-in function is Random(), which returns a real number in the range of 0.0 to 1.0 00092 chosen at random. 00093 00094 *****************************************************************************/ 00095 }} 00096 00098 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:59 $ $Author: dgl $ $Name: $ $Id: _b0115_8cpp-source.html,v 1.4 2006/09/12 17:37:59 dgl Exp $ */ 00099 // The Musimat Tutorial © 2006 Gareth Loy 00100 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00101 // and published exclusively by The MIT Press. 00102 // This program is released WITHOUT ANY WARRANTY; without even the implied 00103 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00104 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00105 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00106 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00107 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00108 // The Musimat website: http://www.musimat.com/ 00109 // This program is released under the terms of the GNU General Public License 00110 // available here: http://www.gnu.org/licenses/gpl.txt 00111
1.4.7