Title: Functions Pass by Value Pass by Reference
1Functions-----------Pass by ValuePass by
Reference
2Arguments vs. Parameters
- Arguments
- function call
- actual values passed to function
- Parameters
- Placeholder in function heading waiting for
arguments
answer cube (x) double cube (double inputVal)
Argument (function call)
parameter (function heading)
3What is the output?
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
Call by Value
??
5
4Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
X
?
results
DoubleIt
5Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
?
DoubleIt
6Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
?
?
DoubleIt
?
y
7Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
X
results
?
?
DoubleIt
?
y
8Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
?
?
DoubleIt
5
9Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
?
?
DoubleIt
5
10
10Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
?
10
DoubleIt
10
11Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
?
10
DoubleIt
10
12Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
X
results
10
10
DoubleIt
13Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
X
results
10
?
DoubleIt
14Call by value Sending a copy
- int DoubleIt (int)
- int main ( )
-
- int x 5
- int results DoubleIt (x)
- cout ltlt x
- return 1
-
- int DoubleIt (int y)
-
- y 2y
- return y
5
X
results
10
?
DoubleIt
15What is the output?
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y) // Function Definition
-
- int z y
- y 2 y
- return y z
Call by Reference
??
14,21
16Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y) // Function Definition
-
- int z y
- y 2 y
- return y z
TripleIt
7
x
17Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y) // Function Definition
-
- int z y
- y 2 y
- return y z
?
results
TripleIt
7
x
18Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
?
TripleIt
7
x
19Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
?
?
TripleIt
7
x
20Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
results
?
?
TripleIt
7
y, x
21Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
?
?
TripleIt
7
7
z
22Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
?
?
TripleIt
7
14
7
z
23Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
?
21
?
TripleIt
14
7
z
24Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
?
21
TripleIt
14
7
z
25Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
results
21
21
TripleIt
14
x
26Call by reference Getting the original
- int TripleIt (int) // Prototype
- int main ( ) // Function Definition
-
- int x 7
- int results TripleIt (x)
- cout ltlt x ltlt , ltlt results
- return 1
-
- int TripleIt (int y)
-
- int z y
- y 2 y
- return y z
results
21
?
TripleIt
14
x