C:/Musimathics_local/Musimat/MusimatTutorial/B0122.cpp File Reference

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0122)
Integer subxy ()
Static Void para1 ()
Void add10 (Integer Reference a, Integer Reference b)
Static Void addExample ()
Static Void para2 ()

Variables

Integer x = 2
Integer y = 3


Function Documentation

Void add10 ( Integer Reference  a,
Integer Reference  b 
)

Definition at line 52 of file B0122.cpp.

Referenced by addExample().

00052                                                      {
00053         a = a + 10;
00054         b = b + 10;
00055 }

Static Void addExample (  ) 

Definition at line 58 of file B0122.cpp.

References add10(), x, and y.

Referenced by MusimatTutorialSection().

00058                          {
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 }

MusimatTutorialSection ( B0122   ) 

Definition at line 2 of file B0122.cpp.

References addExample(), para1(), and para2().

00002                               {
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 }

Static Void para1 (  ) 

Definition at line 24 of file B0122.cpp.

Referenced by MusimatTutorialSection().

00024                     {
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 }

Static Void para2 (  ) 

Definition at line 69 of file B0122.cpp.

Referenced by MusimatTutorialSection(), and para1().

00069                     {
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 }}

Integer subxy (  ) 

Definition at line 20 of file B0122.cpp.

References x, and y.

00020                 { 
00021         Return(x - y); 
00022 }


Variable Documentation

Integer x = 2

Definition at line 17 of file B0122.cpp.

Referenced by addExample(), MusimatTutorialSection(), para1(), realToRational(), subxy(), sumExample2(), and MusimatTutorial::testEuclid().

Integer y = 3

Definition at line 18 of file B0122.cpp.

Referenced by addExample(), MusimatTutorialSection(), realToRational(), subxy(), and sumExample2().


Generated on Fri Sep 8 00:53:01 2006 for MusimatLib by  doxygen 1.4.7