Title: Lecture 18:User-Definded function II(cont.)
1Lecture 18User-Definded function II(cont.)
- Introduction to Computer Science
- Spring 2006
2Parameters Passing
- Pass by value (A formal parameter is a value
parameter) - The value of the corresponding actual parameter
is copied into it - The value parameter has its own copy of the data
- Any modifications to the local copy do not change
the original variable in the calling program - Pass by reference (A formal parameter is a
reference parameter) - An alias of the argument is passed to the called
function. - no copies of the actual parameter are made.
- When references are passed into a function, any
changes made to the references will be seen in
the calling function.
3Parameters Passing
include ltiostreamgtusing namespace stdvoid
swap(int x, int y) int main() int x
4 int y 2 cout ltlt "Before swap, x
is " ltlt x ltlt ", y is " ltlt y ltlt endl swap(x,y)
cout ltlt "After swap, x is " ltlt x ltlt ", y is
" ltlt y ltlt endlvoid swap(int first, int
second) int temp temp
second second first first temp
A formal parameter receives a copy of the content
of corresponding actual parameter. which is
called Pass by value
Any modifications to the local copy do not
change the original variable in the calling
program.
An alias of the argument is passed to the called
function. which is called Pass by reference
When references are passed into a function, any
changes made to the references will be seen in
the calling function.
4include ltiostreamgt using namespace std void
addFirst(int first, int second) void
doubleFirst(int one, int two) void
squareFirst(int ref, int val) int main
() int num 5 addFirst(num,
num) doubleFirst(num, num) squareFirst
(num, num) return 0 void
addFirst(int first, int second) first
first 2 second second 2 void
doubleFirst(int one, int two) one one
2 two two 2 void squareFirst(int
ref, int val) ref ref ref val val
2
// After this statement, num5
// After this statement, num14
// After this statement, num14
// After this statement, num196
//Now first5, second5
//first and second are reference
parameters //first, second and num refer to the
same object
//After this statement, first7, second7
// After this statement, first14, second14
//Now one14, two14
//After this statement, one28, two14
//After this statement, one28, two16
//ref is reference parameters //ref and num refer
to the same object
//Now ref14, val14
//After this statement, ref196, val14
//After this statement, ref196, val16
5Reference Variables as Parameters
- Reference parameters can
- Pass one or more values from a function
- Change the value of the actual parameter
- Reference parameters are useful in three
situations - Returning more than one value
- Changing the actual parameter
- When passing the address would save memory space
and time
6Scope of an Identifier
- The scope of an identifier refers to where in the
program an identifier is accessible - Local identifier - identifiers declared within a
function (or block) - Global identifier identifiers declared outside
of every function definition - C does not allow nested functions
- The definition of one function cannot be included
in the body of another function
7Try 1
include ltiostreamgt using namespace std void
f() int main() int x1
f() void f() coutltltxltltx
Global identifier
Local identifier
Local identifier
Local identifier
Local identifier
Output x2
Output x1
Output x1
Error x in function f() is not
declared.
8Scope of an Identifier (continued)
- Global identifiers (such as variables) are
accessible by a function or a block if - The identifier is declared before the function
definition (block) - The function name is different from the
identifier - All parameters of the function have names
different than the name of the identifier - All local identifiers (such as local variables)
have names different than the name of the
identifier
9Case 1
include ltiostreamgt using namespace std void
f() coutltlt"x"ltltx int x1 int
main() f()
Output x2
Error 'x' redefinition previous definition was
'function'
Error x in function f() is not
declared.
Output x2
The global identifier x can not be accessed by
function f()
The global identifier x can not be accessed by
function f()
The global identifier x can not be accessed by
function x()
The global identifier x can not be accessed by f()
All local identifiers (such as local variables)
should have names different than the name of the
global identifier
All parameters of the function should have names
different than the name of the global identifier
The global identifier should be declared before
the function definition
The function name should be different from the
global identifier
10Scope of an Identifier (continued)
- An identifier declared within a block (Nested
Block) is accessible - Only within the block from the point it is
declared until the end of the block - By those blocks that are nested within that block
if the nested block does not have an identifier
with the same name as that of the outside block
(the block that encloses the nested block) - The scope of a function name is similar to the
scope of an identifier declared outside of any
block
11include ltiostreamgt using namespace std void
f() int x1 int main() int x2
int x3 int x4
int x5 coutltlt"Line 1
"ltltxltltendl coutltlt"Line 2
"ltltxltltendl coutltlt"Line 3
"ltltxltltendl coutltlt"Line 4 "ltltxltltendl
f() void f() coutltlt"Line 5 "ltltxltltendl
Output Line 1 5
Line 2 4
Line 3 3
Line 4 2
Line 5 1
12Global Variables
- Some compilers initialize global variables to
default values - The operator is called the scope resolution
operator - By using the scope resolution operator
- A global variable declared before the definition
of a function (block) can be accessed by the
function (or block) even if the function (or
block) has an identifier with the same name as
the variable - C provides a way to access a global variable
declared after the definition of a function - extern int w
- In this case, the function must not contain any
identifier with the same name as the global
variable
13include ltiostreamgt using namespace std void
f() int x1 int main() f() void
f() int x2 coutltlt "x"ltltx
Output x2
The global identifier x can not be accessed by
function f()
Output x1
All local identifiers (such as local variables)
should have names different than the name of the
global identifier
14Case 1
include ltiostreamgt using namespace std void
f() coutltlt"x"ltltx int x1 int
main() f()
include ltiostreamgt using namespace std void
f() extern int x coutltlt"x"ltltx int
x1 int main() f()
Error x in function f() is not
declared.
Output x1
The global identifier x can not be accessed by f()
The global identifier should be declared before
the function definition
15Side Effects of Global Variables
- Using global variables has side effects
- Any function that uses global variables
- Is not independent
- Usually it cannot be used in more than one
program - If more than one function uses the same global
variable and something goes wrong - It is difficult to find what went wrong and where
- Problems caused by global variables in one area
of a program might be misunderstood as problems
caused in another area
16Static and Automatic Variables
- Automatic variable - memory is allocated at block
entry and deallocated at block exit - Static variable - memory remains allocated as
long as the program executes - Variables declared outside of any block are
static variables - By default variables declared within a block are
automatic variables
17Static and Automatic Variables (continued)
- Declare a static variable within a block by using
the reserved word static - The syntax for declaring a static variable is
- static dataType identifier
- The statement
- static int x
- declares x to be a static variable of the type
int - Static variables declared within a block are
local to the block - Their scope is the same as any other local
identifier of that block
18//Program Static and automatic
variables include ltiostreamgt using namespace
std void test() int main() int
count for (count 1 count lt 5
count) test() return 0 void
test() static int x 0 int y 10 x x
2 y y 1 cout ltlt "x " ltlt x
ltltendl cout ltlt " y " ltlt y ltlt endl
Output x2 y11
x4 y11
x6 y11
x8 y11
x10 y11
19End of lecture 18