Functions

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

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0121)
Integer add (Integer a, Integer b)
Static Void sumExample1 ()
Static Void sumExample2 ()

Function Documentation

Integer add ( Integer  a,
Integer  b 
)

Definition at line 19 of file B0121.cpp.

                                  {                     //return the sum of a plus b
        Integer sum = a + b;
        Return(sum);
}
MusimatTutorialSection ( B0121   )

Definition at line 2 of file B0121.cpp.

References sumExample1(), and sumExample2().

                              {
        Print("*** B.1.21 Scope of Variables ***");
        /*****************************************************************************
         
         B.1.21 Scope of Variables
         
         A function's formal arguments are said to have local scope because they flow into existence when 
         the function begins to execute and cease to exist when the function is finished. 
         
         It is also possible to declare other variables within the body of a function. 
         For example, the following function named add() defines a local variable named sum:
         *****************************************************************************/
        
        sumExample1(); // Step into this function to continue the tutorial
        sumExample2(); // Step into this function to continue the tutorial
}
Static Void sumExample1 (  )

Definition at line 24 of file B0121.cpp.

References add().

                          {
        
        /*****************************************************************************
         Here is an example of calling the add() function.
         *****************************************************************************/
        
        Integer z;
        z = add( 11, 13 );
        Print(z);
}
Static Void sumExample2 (  )

Definition at line 35 of file B0121.cpp.

References x, and y.

                          {
        
        /*****************************************************************************
         Like the formal arguments a and b, the scope of variable sum is local to the function add(). They disappear 
         when the function exits. The only thing that persists is the expression in the Return statement, 
         which is passed back to the caller of the function.
         
         Local variables can also be declared within compound statements. For example,
         *****************************************************************************/
        
        Integer x = 11;
        Integer y = 9;
        
        If (x > 10 And y < 10) {
                Integer sum = x + y;
                Print(sum);
        }
        
        /*****************************************************************************
         These variables disappear when the enclosing compound statement is exited.
         
         Variables declared outside the scope of any function are called global variables. They are acces-
         sible from the point they are declared until the end of the program. They are said to have global 
         scope.
         
         *****************************************************************************/
}