Chapter 7 Malik
Chapter 7 Malik
Chapter 7 Malik
Chapter 7
Chapter Topics
Void Functions Without Parameters
Void Functions With Parameters
Reference Parameters
Value and Reference Parameters and Memory
Allocation
Reference Parameters and Value-Returning
Functions
Scope of an Identifier
Side Effects of Global Variables
Static and Automatic Variables
Function Overloading
Functions with Default Parameters
Void Functions
Void functions do not return a value
Think of them as performing a task
Note
The prototype
The syntax of the declaration, the definition
The syntax of the call
Improving Functions
We
Weneed
needto
tocommunicate
communicateto
to
the
thefunctions
functions
Improving Functions
Sending
Sendingvalues
valuesto
tothe
thefunctions
functions
with
withvalue
valueparameters.
parameters.
Function Parameters
Formal parameter
declared in the function heading
Actual parameter
variable or expression listed in a call
(invocation) of the function
void main ( )
{ . . .
print_summary (rpt_total);
. . .
}
void print_summary (int total)
{ . . .
cout << . . .
}
main ( )
. . .
print_summary (rpt_total);
. . .
}
void print_summary (int total)
{ . . .
cout << . . .
}
10
Function Declarations
11
Compiler
must know about the function
Function
type
Function name
Function Declarations
12
}}
print_summary
print_summary (int
(int total)
total)
.. .. ..
cout
cout <<
<< .. .. ..
This
Thistakes
takescare
careof
of informing
informingthe
the
compiler
compiler of
ofwhat
whatititneeds
needs
and
anddefining
definingthe
thesource
sourcecode
codealso
also
main
main (( ))
.. .. ..
print_summary
print_summary (rpt_total);
(rpt_total);
revenue
revenue == rpt_total
rpt_total ** .72675;
.72675;
.. .. ..
13
Value Parameters
14
main ( )
. . .
print_summary (rpt_total);
. . .
}
void print_summary (int total)
{ . . .
cout << . . .
}
Value Parameter
15
Viewexample
program
}
void print_summary (int total)
{ . . .
cout << . . .
}
Value Parameters
16
Reference Parameters
17
Reference Parameters
ItItwould
wouldbe
behelpful
helpfulto
to
be
beable
ableto
tohave
havethe
thefunctions
functions
communicate
communicateback
backto
tothe
the
calling
callingmodule.
module.
18
Reference Parameters
Reference
ReferenceParameters
Parametersprovide
provide
that
thatcapability
capability
19
Reference Parameters
20
Viewexample
program
21
Reference
Receives address of actual
parameter
Actual parameter must be
a variable
Value can be thought of as
traveling both ways (in
and out)
Formal & actual
parameters must be of
same type
Reference Parameters
Recall our previous model for a function with
value parameters
5
(values go in only)
22
23
24
During execution,
changes made to the formal parameter change the
value of the actual parameter.
25
Changes made to
parameter b will affect
num2 in main
26
27
28
Scope of Identifiers
Scope <=> the region of program code
where it is legal to reference (use) an
identifier
Local scope <=> from where an identifier
is declared, on to the end of the block
Block
void print_max_value
(int value_1, int value_2, int value_3)
{ int hold_value;
hold_value = value_1;
if (value_2 > hold_value)
hold_value = value_2;
. . .
}
Scope of Identifiers
29
int sum,
sum, count,
count, n1,
n1, n2;
n2;
int
void print_totals(
print_totals( int
int amt);
amt);
void
void main
main (( ))
void
{{
.. .. ..
Restof
offile
file
Rest
Name Precedence
Name of a local identifier can be the same as
the name of a global identifier
Name precedence <=> local identifier has
precedence over
global identifier
void print_sum (int n1, int n2)
with same name { int sum;
sum = n1 + n2;
cout << sum; }
void main( )
{
float sum, x, y;
. . .
print_sum (x, 34);
. . .
30
Scope Rules
How to decide where an identifier is
accessible
Look at where it is declared
local (within a block)
global (within the file)
non-local (outside a given block)
31
32
Scope Rules
Function names are global
no such thing as nested functions
33
Side Effects
Any effect of one function on another that
is
not part of the explicitly defined interface
between them
Caused by
careless use of reference parameters
poor design by use of globals in functions
34
Globals in Functions
Assign value to globals
Call function
Use globals
in function
Note -- according
to the instructor
this is generally
a bad practice!
35
36
Globals in Functions
This is a tempting way to go
especially when you dont comprehend
parameters too well!!
Global Constants
Value of a constant cannot be changed
Thus, acceptable to reference named
constants globally
change of the constant can be done in the
source code -- then recompile
that change is then done for all instances of
the identifier
const int lines_per_page = 66;
void main ( )
{ . . .
37
Lifetime of a Variable
38
Lifetime of a Variable
int x=5, y;
void do_it( )
{ int a = 0;
. . . }
void to_it (float b )
{ b = 3 ; . . . }
void main ( )
{ y = 17;
do_it ( );
to_it ( );
cout << x + y;
}
Memory
Locals
Globals
x: 5
y : 17
.exe code
O.S.
39
Lifetime of a Variable
int x=5, y;
void do_it( )
{ int a = 0;
. . . }
void to_it (float b )
{ b = 3 ; . . . }
void main ( )
{ y = 17;
do_it ( );
to_it ( );
cout << x + y;
}
a allocated
Memory
a: 0
Locals
Globals
x: 5
y : 17
.exe code
O.S.
40
Lifetime of a Variable
int x=5, y;
void do_it( )
{ int a = 0;
. . . }
void to_it (float b )
{ b = 3 ; . . . }
void main ( )
{ y = 17;
do_it ( );
to_it ( );
cout << x + y;
}
a de-allocated
Memory
Locals
Globals
x: 5
y : 17
.exe code
O.S.
41
Lifetime of a Variable
int x=5, y;
void do_it( )
{ int a = 0;
. . . }
void to_it (float b )
{ b = 3 ; . . . }
void main ( )
{ y = 17;
do_it ( );
to_it ( );
cout << x + y;
}
b allocated
Memory
b:2
Locals
Globals
x: 5
y : 17
.exe code
O.S.
42
Lifetime of a Variable
int x=5, y;
void do_it( )
{ int a = 0;
. . . }
void to_it (float b )
{ b = 3 ; . . . }
void main ( )
{ y = 17;
do_it ( );
to_it ( );
cout << x + y;
}
b de-allocated
Locals
Globals
x: 5
y : 17
.exe code
O.S.
43
Lifetime of a Variable
Scope is a compile-time issue
Lifetime is a run-time issue
Automatic variable
allocated at block entry
deallocated at exit
Static variable
once allocated remains allocated for whole
program
44
45
46
Static Variables
By default, local variables are automatic.
To obtain a static local variable, you
must use the reserved work static in
its declaration.
47
Whatgets
getsprinted?
printed?
What
48
Function Overloading
49
Function Overloading
50
Instead of:
int largerInt(int x, int y);
char largerChar(char first, char second);
double largerDouble(double u, double v);
string largerString(string first, string second);
51
52