In C++, all variables must be declared prior to their use because the compiler must know what type of data a variable
contains before it can properly compile any statement that uses the variable. Technically, the variable simply has to be declared before it is first used. In C++,
there are seven basic data types: character, wide character, integer, floating point,
double floating point, Boolean, and somewhat surprisingly, valueless. The keywords
used to declare variables of these types in C++ are char, wchar_t, int, float, double,
bool, and void, respectively.
Variables can be localized to a block, namely, a variable can be declared inside any block of code and is then local to it. Note that a block begins
with an opening curly brace and ends with a closing curly brace.
In C++, it is common practice to declare all the variables needed within a function at the
beginning of that function’s code block. The reason is that anyone
reading the code can easily determine what variables are used. However, the
beginning of the function’s block is not the only place where local variables can
be declared. In general, there are three places where variables will be declared:
i) inside functions (called local
variables). Such variables can be used only
by statements that are inside that function and are not known to functions
outside their own. A local variable can normally be declared anywhere, within any block of code. In many cases, variables are not declared until just before they are needed.
In C++, local variables are created when the function is called, and are destroyed when
the function is exited. Therefore, the storage for these local variables is created
and destroyed in the same way. For these reasons, the value of a local variable is lost each
time its function returns.)
In some C++ literature, a local variable is also called a dynamic variable or an automatic
variable.
When a local variable, which is declared in an inner block, has the same name as a variable, which is
declared in an outer block, the variable declared in the inner block will override the one
in the outer block, within the scope of the inner block, for instance (script):
Output:
In the example below, the EMi is declared within the initialization portion of the FOR and is used to control
this loop only. Outside the loop, EMi is unknown (script):
Then, an error message is shown when
it is executed:
In general, when the loop control variable of a FOR is not needed outside the loop, then
declaring it inside the FOR statement is a good idea because
it localizes the variable to the loop to avoid its accidental misuse elsewhere.
ii) in the definition
of function parameters (called formal parameters).
If a function has arguments, then those arguments must
be declared. That is, if a function uses arguments, it must declare variables that will
accept the values of those arguments. These arguments are called the formal parameters of the function. The formal
parameters behave like any other local variables inside the function. Therefore, you must make sure that the formal parameters, which you declare, are of the same type as the
arguments you will pass to the function. This declaration occurs after the function name, inside the
parentheses:
EMFunction (int first, int last, char ch)
{
.
.
.
}
The EMFunction ( ) function has three arguments, called first, last, and ch. Then, you must tell
C++ what type of variables these are by declaring them so that these arguments can receive information passed to the function. Note that, like the other local variables, the values of the variables are lost once the function terminates.
iii) outside of all functions (called global variables).
Global variables are, in general, the opposite of local variables. The data of a global variable stay in existence throughout
the entire execution of your program. The storage for global variables is correspondingly in a fixed region of memory set aside for the purpose
of the program. Global variables can be created by declaring them outside of all functions. A global variable can then be accessed by any function. If a global variable
and a local variable have the same name, all references to that variable name inside
the function, in which the local variable is declared, will refer to the local variable and
not to the global variable. That is, when a global and a local variable share the same name, then the local variable has
precedence, for example1 and example2 below:
Table 1144a. Comparison between among local
variables, formal parameters and global variables. |
|
Local
variables |
Formal parameters |
Global variables |
Initialization |
Are initialized each time the function in which they are declared is entered |
|
Are initialized only at the start of the program |
Are
not initialized will have unknown values before the first assignment is made to them. |
|
Are initialized to zero if no other initializer is specified |
Unnecessary global variables should be avoided mainly for three reasons:
◆ Such variables take up PC memory whenever the program is executing, not just when
they are needed.
◆ Using a global variable, where a local variable is sufficient, makes a function less
efficient.
◆ Using a large number of global variables can result in program errors because of
unknown, and unwanted, side effects.
The value to a variable can be assigned by directly placing an
equal sign and the value after the variable name. For instance, variable_name = value. Although variables are frequently initialized by constants, a variable can also be initialized by
using any expression valid at the time of the initialization. Note the results of two scripts in Table 1144b are different.
Table 1144b. Comparison between two results from two different scripts.
Script A |
Script B |
number EMa
number EMb
EMb = EMa
EMa = 2
result (EMb + "\n\n") |
number EMa
number EMb
EMa = 2
EMb = EMa
result (EMb + "\n\n") |
|