Functions | Variables

/Users/garethloy/Musimathics/Musimat1.2/MusimatTutorial/B0122.cpp File Reference

#include "MusimatTutorial.h"

Go to the source code of this file.


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


Integer x = 2
Integer y = 3

Function Documentation

Void add10 ( Integer Reference  a,
Integer Reference  b 

Definition at line 55 of file B0122.cpp.

        a = a + 10;
        b = b + 10;
Static Void addExample (  )

Definition at line 61 of file B0122.cpp.

References add10(), x, and y.

         Now let's use the two global variables defined above as actual arguments to 
         the function and then print their values:
        add10(x, y);
MusimatTutorialSection ( B0122   )

Definition at line 2 of file B0122.cpp.

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

        Print("*** B.1.22 Pass by Value vs. Pass by Reference ***");
         B.1.22 Pass by Value vs. Pass by Reference
         Global variables can be accessed directly within functions. For example, this function returns the 
         difference of global variables x and y.
        para1();                // Step into this function to continue the tutorial
        addExample();   // Step into this function to continue the tutorial
        para2();                // Step into this function to continue the tutorial
Static Void para1 (  )

Definition at line 25 of file B0122.cpp.

         The above definitions of x and y are global because they occur outside the scope of a compound block or function.
         The function subxy() accesses these global variables directly and forms their difference.
         Referencing global variables directly inside a function is not a recommended practice because 
         it ties the function to particular individual variables, limiting its usefulness.
         The reason people are tempted to reference global variables directly inside functions is that ordi-
         narily all that returns from a function is the expression in its Return() statement. Sometimes, it's 
         nice to allow a function to have additional side effects. That way functions can affect more than 
         one thing at a time in the program. But there's a better way to accomplish side effects: we can use 
         arguments to pass in a reference to a variable from outside.
         As described in the preceding section, ordinarily only the value is copied from an actual argu-
         ment to its corresponding formal argument. But declaring a formal argument to be of type 
         Reference causes Musimat to let the function directly manipulate a variable supplied as an 
         actual argument. The function doesn't get the value of the variable, it gets the variable itself. When 
         a function changes a Reference formal argument, it changes the variable supplied as the actual 
         We can use Reference arguments to allow functions to have multiple effects on the variables 
         in a program. For example, let's declare a function that takes two Reference arguments and adds 
         10 to each of their values.
Static Void para2 (  )

Definition at line 73 of file B0122.cpp.

         This prints 12 and 13 because the function changed the values of both global variables. This is a 
         very handy trick.
         Here are the rules to remember:
         o An ordinary (non-Reference) formal argument provides its function with a copy of its actual 
         argument. Changing the value of an ordinary (non-Reference) formal argument inside the func-
         tion does not change anything outside the function, that is, such arguments have local scope. The 
         actual arguments are said to be passed by value to the formal arguments. 
         o A Reference formal argument provides its function with direct access to the variable named 
         as its actual argument. The actual argument must be a variable. Modifying the value of a 
         Reference argument inside a function changes the referenced variable outside the function. 
         Thus, the scope of a Reference formal argument is the same as the scope of its actual argument. 
         The actual arguments are said to be passed by reference to formal arguments when they are 
         declared to be of type Reference.
Integer subxy (  )

Definition at line 21 of file B0122.cpp.

References x, and y.

        Return(x - y); 

Variable Documentation

Integer x = 2

Definition at line 18 of file B0122.cpp.

Integer y = 3

Definition at line 19 of file B0122.cpp.