C++ Programming
Lecture 11
Functions – Part III
The Hashemite University
Computer Engineering
Department
(Adapted from the textbook slides)
Outline
Introduction.
Identifiers storage classes.
Identifiers scope rules.
The Hashemite University
2
Introduction
The main attributes attached with any variable
or identifier include:
Name, type, size, value (as taken before).
Storage class
Scope
Determines the period during which the variable exists in
memory
Where the identifier can be referenced in program
Linkage
Where an identifier is known in multiple source file programs.
Determine whether an identifier is known only in the current
source file or in any source file
The Hashemite University
3
Storage Classes
Storage classes types:
Automatic storage.
Static storage.
Five storage-class specifiers (or keywords
used in C++):
auto,
register,
extern,
static.
mutable,
Automatic
storage
Static storage
The Hashemite University
4
Automatic Storage
Variables created and destroyed within its block
(created when entering the block and destroyed
when leaving this block).
Can only be used with local variables and
parameters of a specified function.
Two types:
auto
Default for local variables.
Example:
auto float x, y;
register
Tries to put variables into high-speed registers.
The compiler can ignore it if no sufficient number of
registers are available.
Not necessary in optimized compilers.
The Hashemite University
5
Static Storage
Variables exist from the point of declaration for entire program
execution.
Static variables are created and initialized once when the program
begins execution.
Numeric variables are initialized to 0 by default unless the programmer
initialize them explicitly.
Two types:
static
Usually, used with local variables defined in functions
Permanent storage for the static variable is allocated, so that it is not
removed from memory when leaving the block it is defined on.
Keep value after function ends
Only known in their own function (known inside its function like auto
and register but it is not removed from memory when the function exit).
extern
Default for global variables and functions.
used to specify that the variable is declared in a different file.
Known in any function.
The Hashemite University
6
Storage Classes -- Notes
Do not use multiple storage specifiers at the
same time syntax error.
E.g.: register auto double x = 0; // syntax error
mutable storage class is used with classes (in
the object oriented classes).
Mutable keyword can only be applied to non-static
class members
The Hashemite University
7
Identifier Scope Rules I
Scope is where the identifier can be
referenced in program (The portion of the program where
an identifier can be used).
Scope Types:
File scope:
Defined outside a function, known in all functions
Known in all functions from the point at which the identifier
is declared until the end of the file
Examples include, global variables, function definitions and
functions prototypes
Function scope:
Can only be referenced inside a function body
The Hashemite University
8
Identifier Scope Rules II
Block scope:
Function prototype scope:
Declared inside a block. Begins at declaration, ends at }.
Includes variables, function parameters (local variables of
function).
If two nested blocks have the same variable, outer blocks
“hidden” from inner blocks.
Identifiers in parameter list
Names in function prototype optional, and can be used
anywhere
Class and namespace scopes: will be taken later.
The Hashemite University
9
Summary
Global variables are by default static
Local variable are by default auto
You cant reference (print/modify) local
variable outside its scope
Examples
bool fun()
{
static int var=-1; //line 3
}
int main()
{
cout<
var is local to fun()
}
var is static so it will be
stored in memory
from line 3 to the end
of the code, but you
cant print it outside
fun()
static int var=-1; //line 1
bool fun()
{
cout<
}
int main()
{
cout<
}
bool fun()
{
cout<
var is not defined yet
}
static int var=-1; //line 5
int main()
{
cout<
var is static so it will
be stored in
var is static and global
memory from line 5
so it will be stored in
to the end of the
memory from line 1
code, and you can
to the end of the
print it from line 5
code, and you can
to the end of the
print it anywhere in
code
Hashemite University
10
yourThe
code
Examples (2)
int fun1(int x);
int main ()
{
cout<
fun1(5); // function name is file scope you can access it anywhere in you code
after the prototype
return 0;
}
void fun1(int x1)
{
x1=5;
cout<
cout<
}
Inside any scope (function scope or block scope) you cant define
two variables with the same names
The Hashemite University
11
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Fig. 3.12: fig03_12.cpp
// A scoping example
#include <iostream>
using std::cout;
using std::endl;
void a( void );
void b( void );
void c( void );
// function prototype
// function prototype
// function prototype
x is different inside and outside
variable
the block.
int x = 1;
// global
int main()
{
int x = 5;
// local variable to main
cout << "local x in outer scope of main is " << x << endl;
{
// start new scope
int x = 7;
}
cout << "local x in inner scope of main is " << x << endl;
// end new scope
cout << "local x in outer scope of main is " << x << endl;
a();
b();
c();
a();
b();
c();
//
//
//
//
//
//
a has automatic local x
b has static locallocal
x
x in outer scope of main is 5
c uses global x
local x in inner scope of main is 7
a reinitializes automatic
local
x scope of main is 5
local x in
outer
static local x retains its previous value
The
University
global x also retains
itsHashemite
value
12
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
cout << "local x in main is " << x << endl;
return 0;
Local automatic variables are
created and destroyed each
time a is called.
}
void a( void )
{
int x = 25;
cout <<
<<
++x;
cout <<
<<
// initialized each time a is called
endl << "local x in a is " << x
" after entering a" << endl;
"local x in a is " << x
" before exiting a" << endl;
local x in a is 25 after entering a
local x in a is 26 before exiting a
}
void b( void )
{
static int x = 50;
Local static variables are
// Static initialization only
not destroyed when the
// first time b is called.
function ends.
endl << "local static x is " << x
" on entering b" << endl;
local static x is 50 on entering b
"local static x is " << x
local static x is 51 on exiting b
" on exiting b" << endl;
3.1 Define Functions
cout <<
<<
++x;
cout <<
<<
}
Global variables are always
accessible. Function c references
the global x.
void c( void )
{
cout << endl << "global x is " << x
global x is 1 on entering c
<< " on entering c" << endl;
global x is 10 on exiting c
x *= 10;
Hashemite
University
cout << "global x is " << x << " on The
exiting
c" << endl;
}
13
local x in outer scope of main is 5
local x in inner scope of main is 7
local x in outer scope of main is 5
local x in a is 25 after entering a
local x in a is 26 before exiting a
local static x is 50 on entering b
local static x is 51 on exiting b
global x is 1 on entering c
global x is 10 on exiting c
local x in a is 25 after entering a
local x in a is 26 before exiting a
local static x is 51 on entering b
local static x is 52 on exiting b
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5
The Hashemite University
14
Unary Scope Resolution
Operator
Unary scope resolution operator (::)
If a global variable and a local variable share the
same name, unary scope resolution operator is used
to access the global variables.
Not needed if names are different
Instead of variable use ::variable
Very important Cannot be used to access a local
variable of the same name in an outer block.
The Hashemite University
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Fig. 3.24: fig03_24.cpp
// Using the unary scope resolution operator
#include <iostream>
using std::cout;
using std::endl;
const double PI = 3.14;
int main()
{
const float PI = 3.14159265358979;
Notice the use of ::
cout << " Local float value of PI = " << PI
<< "\nGlobal double value of PI = " << ::PI << endl;
return 0;
}
Local float value of PI = 3.14159265358979
Global double value of PI = 3.14
The Hashemite University
16
Additional Notes
This lecture covers the following material from the textbook:
Fourth Edition
Chapter 3: Sections 3.10, 3.11, and 3.19
The Hashemite University
17