- Main Page
- Namespaces
- Files

- File List
- File Members

# C:/Musimathics_local/Musimat/MusimatTutorial/B0121.cpp

Go to the documentation of this file.

    00001 #include "MusimatTutorial.h"
    00002 MusimatTutorialSection(B0121) {
    00003 Print("*** B.1.21 Scope of Variables ***");
    00004 /*****************************************************************************
    00005
    00006 B.1.21 Scope of Variables
    00007
    00008 A function’s formal arguments are said to have local scope because they flow into existence when
    00009 the function begins to execute and cease to exist when the function is finished.
    00010
    00011 It is also possible to declare other variables within the body of a function.
    00012 For example, the following function named add() defines a local variable named sum:
    00013 *****************************************************************************/
    00014         sumExample1(); // Step into this function to continue the tutorial
    00015         sumExample2(); // Step into this function to continue the tutorial
    00016 }
    00017
    00018 Integer add(Integer a, Integer b) {                     //return the sum of a plus b
    00019         Integer sum = a + b;
    00020         Return(sum);
    00021 }
    00022
    00023 Static Void sumExample1() {
    00024 /*****************************************************************************
    00025 Here is an example of calling the add() function.
    00026 *****************************************************************************/
    00027
    00028         Integer z;
    00029         z = add( 11, 13 );
    00030         Print(z);
    00031 }
    00032
    00033 Static Void sumExample2() {
    00034 /*****************************************************************************
    00035 Like the formal arguments a and b, the scope of variable sum is local to the function add(). They disappear
    00036 when the function exits. The only thing that persists is the expression in the Return statement,
    00037 which is passed back to the caller of the function.
    00038
    00039 Local variables can also be declared within compound statements. For example,
    00040 *****************************************************************************/
    00041
    00042         Integer x = 11;
    00043         Integer y = 9;
    00044
    00045         If (x > 10 And y < 10){
    00046                 Integer sum = x + y;
    00047                 Print(sum);
    00048         }
    00049
    00050 /*****************************************************************************
    00051 These variables disappear when the enclosing compound statement is exited.
    00052
    00053 Variables declared outside the scope of any function are called global variables. They are acces-
    00054 sible from the point they are declared until the end of the program. They are said to have global
    00055 scope.
    00056
    00057 *****************************************************************************/
    00058 }}
    00059
    00061 /* $Revision: 1.2 $ $Date: 2006/09/09 06:23:09 $ $Author: dgl $ $Name:  $ $Id: _b0121_8cpp-source.html,v 1.2 2006/09/09 06:23:09 dgl Exp $ */
    00062 // The Musimat Tutorial © 2006 Gareth Loy
    00063 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy
    00064 // and published exclusively by The MIT Press.
    00065 // This program is released WITHOUT ANY WARRANTY; without even the implied
    00066 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00067 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
    00068 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
    00069 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
    00070 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
    00071 // The Musimat website:                 http://www.musimat.com/
    00072 // This program is released under the terms of the GNU General Public License
    00073 // available here:                      http://www.gnu.org/licenses/gpl.txt
    00074