00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0122) { 00003 Print("*** B.1.22 Pass by Value vs. Pass by Reference ***"); 00004 /***************************************************************************** 00005 00006 B.1.22 Pass by Value vs. Pass by Reference 00007 00008 Global variables can be accessed directly within functions. For example, this function returns the 00009 difference of global variables x and y. 00010 *****************************************************************************/ 00011 00012 para1(); // Step into this function to continue the tutorial 00013 addExample(); // Step into this function to continue the tutorial 00014 para2(); // Step into this function to continue the tutorial 00015 } 00016 00017 00018 Integer x = 2; // x is a global variable 00019 Integer y = 3; // y is a global variable 00020 00021 Integer subxy() { 00022 Return(x - y); 00023 } 00024 00025 Static Void para1() { 00026 00027 /***************************************************************************** 00028 The above definitions of x and y are global because they occur outside the scope of a compound block or function. 00029 The function subxy() accesses these global variables directly and forms their difference. 00030 00031 Referencing global variables directly inside a function is not a recommended practice because 00032 it ties the function to particular individual variables, limiting its usefulness. 00033 00034 The reason people are tempted to reference global variables directly inside functions is that ordi- 00035 narily all that returns from a function is the expression in its Return() statement. Sometimes, it's 00036 nice to allow a function to have additional side effects. That way functions can affect more than 00037 one thing at a time in the program. But there's a better way to accomplish side effects: we can use 00038 arguments to pass in a reference to a variable from outside. 00039 00040 As described in the preceding section, ordinarily only the value is copied from an actual argu- 00041 ment to its corresponding formal argument. But declaring a formal argument to be of type 00042 Reference causes Musimat to let the function directly manipulate a variable supplied as an 00043 actual argument. The function doesn't get the value of the variable, it gets the variable itself. When 00044 a function changes a Reference formal argument, it changes the variable supplied as the actual 00045 argument. 00046 00047 We can use Reference arguments to allow functions to have multiple effects on the variables 00048 in a program. For example, let's declare a function that takes two Reference arguments and adds 00049 10 to each of their values. 00050 *****************************************************************************/ 00051 00052 } 00053 00054 00055 Void add10(Integer Reference a, Integer Reference b) { 00056 a = a + 10; 00057 b = b + 10; 00058 } 00059 00060 00061 Static Void addExample() { 00062 00063 /***************************************************************************** 00064 Now let's use the two global variables defined above as actual arguments to 00065 the function and then print their values: 00066 *****************************************************************************/ 00067 00068 add10(x, y); 00069 Print(x); 00070 Print(y); 00071 } 00072 00073 Static Void para2() { 00074 00075 /***************************************************************************** 00076 This prints 12 and 13 because the function changed the values of both global variables. This is a 00077 very handy trick. 00078 00079 Here are the rules to remember: 00080 00081 o An ordinary (non-Reference) formal argument provides its function with a copy of its actual 00082 argument. Changing the value of an ordinary (non-Reference) formal argument inside the func- 00083 tion does not change anything outside the function, that is, such arguments have local scope. The 00084 actual arguments are said to be passed by value to the formal arguments. 00085 00086 o A Reference formal argument provides its function with direct access to the variable named 00087 as its actual argument. The actual argument must be a variable. Modifying the value of a 00088 Reference argument inside a function changes the referenced variable outside the function. 00089 Thus, the scope of a Reference formal argument is the same as the scope of its actual argument. 00090 The actual arguments are said to be passed by reference to formal arguments when they are 00091 declared to be of type Reference. 00092 00093 *****************************************************************************/ 00094 } 00095 00097 /* $Revision: 1.3 $ $Date: 2006/09/05 08:03:08 $ $Author: dgl $ $Name: $ $Id: B0122.cpp,v 1.3 2006/09/05 08:03:08 dgl Exp $ */ 00098 // The Musimat Tutorial © 2006 Gareth Loy 00099 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00100 // and published exclusively by The MIT Press. 00101 // This program is released WITHOUT ANY WARRANTY; without even the implied 00102 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00103 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00104 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00105 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00106 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00107 // The Musimat website: http://www.musimat.com/ 00108 // This program is released under the terms of the GNU General Public License 00109 // available here: http://www.gnu.org/licenses/gpl.txt 00110