9.5 Functions and Variables


9.5 Functions and Variables

The example programs shown in the previous chapters have only been seen to exist within the confines of the main function. In this chapter, we've demonstrated how functions of all kinds can be defined and called, and that functions can call other functions too. In other words, by calling functions, it's possible to control the flow of program execution outside of the main function. You can call a function. This in turn can call other functions, and so on. Thus, there is the potential for program structure to become confusing if it is not organized carefully. For this reason, program design becomes important.

One of the significant factors related to functions is how they affect variables, and how variables can affect functions. As one example, we saw earlier that For loops were used, and in each of the loops there was a variable called "counter." The question is, if there is more than one variable with the same name "counter," then how does the program distinguish them as separate counters? How does it know which is which and why aren't they confused? The answer is related to variable scope.

9.5.1 Variable Scope

The following sample program contains a number of variables-one that is declared outside any function and others that are declared inside functions. Some of the variables declared inside functions share the same name, but the compiler treats them as separate variables and can distinguish one from the others. Consider the following code and let's see how this works.

      #include <iostream>      char the_big_letter;      int My_Sample_Function()      {         int Result = 0;         for(int counter = 0; counter < 10; counter++)         {            int variable = 0;         }         the_big_letter = 'E';         return Result;      }      int main()      {         int Result = 1;         the_big_letter = 'C';         My_Sample_Function();         return Result;      } 

9.5.2 Global, Local, and Block Variables

Variables come in two main forms-global and local-and they all have a finite lifespan. We'll refer to the code in the previous section to help illustrate their use.

  • Global variables

    A global variable is one that is declared outside the range of any function. Its lifespan is for the entire program as it is created as the program begins and destroyed as the program ends. From the code sample above, char the_big_letter is an example of this. Since a global variable is active throughout the whole program, it can be used in any function. Its name must therefore be unique, and wherever it is referenced in an application, it will specifically refer to that one variable.

    image from book
          char global_variable1; //Declared outside functions      int function1()      {      ...      }      int function2()      {      ...      } 
    image from book

  • Local variables

    If a variable is declared inside a function, then it is a local variable. Its lifespan is for the duration of the function, which means that as the function ends, the variable is destroyed in memory. It can be accessed anywhere within the function. If the function runs more than once, a new variable is created and destroyed each time the function runs. From the earlier code sample, int Result is an example of a local variable. A variable with this name is declared in each function, and each one is a separate variable whose scope and existence is confined within that function only. Local variables cannot be accessed outside of that function or by other functions or processes.

    image from book
          int function1()      {      char local_variable;      }      int function2()      {      char local_variable;      } 
    image from book

  • Block variables

    Block variables are a special type of local variable. From the code sample, int counter is an example of a block variable, declared for each For loop. Block variables have an even narrower scope than local. These variables have a scope only within a specific block of code, like a loop block or the block of an If statement. Thus, the variable counter, and any other variables declared inside a block, can only be accessed within that block. They have no scope outside of that block.

    image from book
          int function1()      {       if(condition)       {          char block_variable;       }      } 
    image from book

Tip 

Remember, that global variables can be accessed anywhere, local variables can be accessed only in functions (and also within any blocks of that function), and block variables can be accessed only within a specific block and any subblocks it might have.

Note 

To conserve memory and ensure efficient operation, programmers are advised to use variables with the shortest scope possible for their specific task.




Introduction to Game Programming with C++
Introduction to Game Programming with C++ (Wordware Game Developers Library)
ISBN: 1598220322
EAN: 2147483647
Year: 2007
Pages: 225
Authors: Alan Thorn

Similar book on Amazon
Beginning C++ Through Game Programming
Beginning C++ Through Game Programming
Beginning Game Programming
Beginning Game Programming
The Ultimate Guide to Video Game Writing and Design
The Ultimate Guide to Video Game Writing and Design

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net