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