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