Functions Pass by Value Pass by Reference - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Functions Pass by Value Pass by Reference

Description:

return y z; x. 7. int TripleIt (int&); // Prototype. int main ( ) // Function Definition ... return y z; results. y, x. X's address is referenced. TripleIt ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 27
Provided by: McK88
Category:

less

Transcript and Presenter's Notes

Title: Functions Pass by Value Pass by Reference


1
Functions-----------Pass by ValuePass by
Reference
  • SI 204

2
Arguments 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)
3
What 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
4
Call 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
5
Call 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
6
Call 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
7
Call 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
8
Call 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
9
Call 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
10
Call 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
11
Call 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
12
Call 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
13
Call 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
14
Call 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
15
What 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
16
Call 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
17
Call 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
18
Call 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
19
Call 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
20
Call 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
21
Call 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
22
Call 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
23
Call 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
24
Call 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
25
Call 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
26
Call 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
Write a Comment
User Comments (0)
About PowerShow.com