Download presentation
Presentation is loading. Please wait.
1
Functions ----------- Pass by Value Pass by Reference IC 210
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 << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; } ?? 5 Call by Value
4
X results Call by value: “Sending a copy” 5 DoubleIt ? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
5
X results Call by value: “Sending a copy” 5 ? ? DoubleIt int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
6
X results Call by value: “Sending a copy” 5 ? ? DoubleIt int DoubleIt (int); int main ( ) { int x = 5; results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; } y ?
7
X results y Call by value: “Sending a copy” 5 ? ? ? X’s value is copied into Y DoubleIt int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
8
X results y Call by value: “Sending a copy” 5 ? ? 5 DoubleIt int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
9
X results y Call by value: “Sending a copy” 5 5 ? 10 DoubleIt ? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
10
X results y Call by value: “Sending a copy” 10 DoubleIt 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; } 5 ?
11
X results y Call by value: “Sending a copy” 5 ? DoubleIt 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
12
X results Call by value: “Sending a copy” 5 10 DoubleIt 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
13
X results Call by value: “Sending a copy” 5 10 DoubleIt ? int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
14
X results Call by value: “Sending a copy” 5 DoubleIt ? 10 int DoubleIt (int); int main ( ) { int x = 5; int results = DoubleIt (x); cout << x; return 1; } int DoubleIt (int y) } y = 2*y; return y; }
15
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) // Function Definition { int z = y; y = 2 * y; return y + z; } What is the output? ?? 14,21 Call by Reference
16
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) // Function Definition { int z = y; y = 2 * y; return y + z; } Call by reference: “Getting the original” TripleIt x 7
17
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) // Function Definition { int z = y; y = 2 * y; return y + z; } results Call by reference: “Getting the original” TripleIt ? x 7
18
results ? ? TripleIt Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } x 7
19
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results ? ? TripleIt Call by reference: “Getting the original” x 7
20
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? ? X’s address is referenced TripleIt Call by reference: “Getting the original” 7
21
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? TripleIt ? Call by reference: “Getting the original” z 7 7
22
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? TripleIt ? Call by reference: “Getting the original” z 7 714
23
int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } results y, x ? TripleIt 14 ? Call by reference: “Getting the original” z 7 21
24
results y, x ? TripleIt 21 14 Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } z 7 _
25
results 21 TripleIt 21 Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } x 14
26
results 21 TripleIt ? Call by reference: “Getting the original” int TripleIt (int&); // Prototype int main ( ) // Function Definition { int x = 7; int results = TripleIt (x); cout << x << “,“ << results; return 1; } int TripleIt (int& y) { int z = y; y = 2 * y; return y + z; } x 14
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.