PF Lecture 6b
PF Lecture 6b
Fundamentals
Lecture 06b
Variable Storage Class
• The scope of a variable determines which parts of the program can access it,
and its storage class determines how long it stays in existence.
• Variables defined inside a block of code have block scope. A block is basically
the code between an opening brace and a closing brace. Thus, a function body
is a block.
• Such variables are called local variables or automatic variables (sometimes).
• Variables defines defined outside the functions are called global variables and
have global scope or file scope.
• There are two storage classes: automatic and static.
• Variables with storage class automatic exist during the lifetime of the
function in which they’re defined.
• Variables with storage class static exist for the lifetime of the program.
Automatic Storage Class
• A local variable is not created until the function in which it is defined is called.
• More accurately, variables defined within any block of code are not created
until the block is executed.
• Thus, variables defined within a loop body only exist while the loop is
executing.
• When a function is called , control is transferred to function definition , the
variables are created and memory space is set aside for them.
• After the execution of return statement, control is passed back to the calling
program, the variables are destroyed, and their values are lost.
• The name automatic is used because the variables are automatically created
when a function is called and automatically destroyed when it returns.
Automatic Storage Class
• The time between the creation and destruction of a variable is called its
lifetime (or sometimes its duration).
• The lifetime of a local variable coincides with the time when the function in
which it is defined is executing.
• The idea behind limiting the lifetime of variables is to save memory space.
• If a function is not executing, the variables it uses during execution are
presumably not needed. Removing them frees up memory that can then be
used by other functions.
• When a local variable is created, the compiler does not try to initialize it.
• Thus, it will start off with an arbitrary value, which may be 0 but probably will
be something else.
Global Variables
• If a global variable is initialized, as in
int exvar = 199;
this initialization takes place when the program is first loaded.
• If a global variable is not initialized explicitly by the program—for example, if it
is defined as
int exvar;
then it is initialized automatically to 0 when it is created.
• This is unlike local variables, which are not initialized and probably contain
random or garbage values when they are created.
• Global variables have storage class static, which means they exist for the life of
the program.
Global Variables
• Memory space is set aside for them when the program begins, and continues
to exist until the program ends.
• You don’t need to use the keyword static when declaring global variables; they
are given this storage class automatically.
Static Local Variables
• Another kind of variable: the static local variable.
• There are static global variables, but they are meaningful only in multifile
programs.
• A static local variable has the visibility of an automatic local variable (that is,
inside the function containing it).
• However, its lifetime is the same as that of a global variable, except that it
doesn’t come into existence until the first call to the function containing it.
• Thereafter it remains in existence for the life of the program.
• Static local variables are used when it’s necessary for a function to remember
a value when it is not being executed; that is, between calls to the function.
• In the next example, a function, getavg(), calculates a running average. It
remembers the total of the numbers it has averaged before, and how many
there were.
Example // getavg()
// finds average of old plus new data
#include <iostream>
using namespace std; float getavg(float newdata)
float getavg(float); //declaration {
int main() static float total = 0; //static variables
{ are initialized
float data=1, avg; static int count = 0; // only once per
while( data != 0 )
{ program
cout << “Enter a number: “; count++; //increment count
cin >> data; total += newdata; //add new data to
avg = getavg(data); total
cout << “New average is “ << avg << endl;
} return total / count; //return the new
return 0; average
} }
//-----------------------------------------------------------
---
Variable Storage types
summary
Passing Arrays to functions
• Array elements can be passed to a called function in the same manner as scalar
variables; they’re simply included as subscripted variables when the function
call is made.
findMin(volts[2], volts[6]);
• Passing a complete array of values to a function is, in many respects, easier
than passing each element.
• The called function receives access to the actual array rather than a copy of
values in the array.
• For example, if volts is an array, the function call findMax(volts); makes the
complete volts array available to the findMax() function.
• This function call is different from passing a single variable to a function.
Example
#include <iostream>
using namespace std;
const int MAXELS = 5;
int findMax(int [MAXELS]); // function prototype
int main()
{
int nums[MAXELS] = {2, 18, 1, 27, 16};
cout << "The maximum value is " << findMax(nums) << endl;
return 0;
}
• Both function headings have the same name and same formal parameter list.
Therefore, these function headings to overload the function functionABC are incorrect.
• In this case, the compiler will generate a syntax error.