Presentation is loading. Please wait.

Presentation is loading. Please wait.

Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.

Similar presentations


Presentation on theme: "Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and."— Presentation transcript:

1 Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and lasts until the end, it is taking up lots of memory –difficult to debug

2 Methods of variable creation (continued) Local on the fly variables –simply created when they are needed, –only available from within the routine in which they were created –memory requirement is less –easy to debug

3 Methods of variable creation (continued) Local defined variable –created before they are needed –only available in the routine in which they were created. –Memory requirement is less –easy to debug –the most usually favored method

4 Scope The scope of an identifier is the portion of the program in which the identifier can be referenced. Some identifiers can be referenced throughout the program. Others can be referenced from only portions of a program. Example: When we declare a local variable in a block, it can be referenced only in that block or in blocks nested within that block.

5 A program written in C++ 1 // This program calculates the number calories in a cheese sandwich 2 #include 3 const int BREAD = 63; // global constant 4 const int CHEESE = 106; // global constant 5 const int MAYONNAISE = 49; // global constant 6 const int PICKLES = 25; // global constant 7 int main() 8 { 9 int totalCalories; // local variable 10 totalCalories = 2 * BREAD + CHEESE + MAYONNAISE + PICKLES; 11 cout << "There were " << totalCalories; 12 cout << " calories in my lunch yesterday."<< endl; 13 return 0; 14 }

6 Function definition and function prototype // A programmer-defined square function #include int square (int); // Function prototype int main() { int x; //local variable for (x = 1; x <= 10; x++) cout << square(x) << ‘ ‘; cout << endl; return 0; } int square (int y) // Function definition { return y * y; } Local variable

7 Storage duration Period during which an identifier exists in memory. Some identifiers exists briefly. Some are repeatedly created and destroyed. Others exist for the entire execution of a program. Storage duration can be of two types: –automatic (auto and register) –static

8 Global and Local declarations #include void func( float ); const int a = 17; // global constant int b; // global variable int c; // global variable int main() { b = 4; // assignment to global b c = 6; // assignment to global c func(42.8); return 0; } void func( float c) // prevents access to global c { float b; // prevent access to global b b = 2.3; // assignment to local b cout << “ a = “ << a; // output global a (17) cout << “ b = “ << b; // output local b (2.3 cout << “ c = “ << c; // output local c (42.8) } Output: A = 17 b = 2.3 c = 42.8

9 Explanation In this example, function func accesses global constant a. However, func declares its own local variable b and parameter c. Local variable b takes precedence over global variable b, effectively hiding global variable b from the statements in function func. Function parameter acts like local variable.

10 Life time of a variable Local variables: variables declared inside a function or variables declared as function parameter. When you will call the function, memory will be allocated for all local variables defined inside the function. Finally memory will be deallocated when the function exits. The period of time a variable will be “alive” while the function is executing is called “lifetime” of this variable.

11 Storage Classes C++ provides four storage classes: –auto –register –extern –static An identifier’s storage class helps determine its storage duration and scope.

12 Auto Storage class Formal parameters and local variables of functions are variables that are automatically allocated on the stack when a function is called and automatically deallocated when the function returns. They are of storage class auto.

13 Extern Storage class of names known to the linker. Example: extern int square (int x); Means the function will be available to the linker. It notifies the compiler that such a function exists and that the linker will know where to find it.

14 Register If you declare a variable of type register, it simply alerts the compiler to the fact that this memory cell will be referenced more often than most. Register is a special high-speed memory location inside the central processor.

15 Static Static variable is allocated and initialized one time, prior to program execution. It remains allocated until the entire program terminates.

16 Namespaces (page 400) Namespace means scope. In C++, namespace is a mechanism by which a programmer can create a named scope. For example, the standard header file cstdlib contains function prototypes for several library functions, one of which is the absolute value function, abs. namespace std {.. int abs (int );.. }

17 Namespace Identifiers declared inside the namespace body have namespace scope. Identifiers defined inside the body can be accessed by three ways. First method: #include int main() { int alpha; int beta;.. alpha = std::abs(beta); } Scope resolution operator

18 Namespace Second Method: #include int main() { int alpha; int beta; using std::abs; … alpha = abs(beta); … }

19 Namespace Third Method: #include int main() { int alpha; int beta; using namespace std; … alpha = abs(beta); … }


Download ppt "Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and."

Similar presentations


Ads by Google