functions ----------- pass by value pass by reference ic 210
Post on 21-Dec-2015
221 views
TRANSCRIPT
![Page 1: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/1.jpg)
Functions-----------
Pass by ValuePass by Reference
IC 210
![Page 2: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/2.jpg)
Arguments vs. ParametersArguments:
• 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)
![Page 3: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/3.jpg)
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
![Page 4: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/4.jpg)
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;}
![Page 5: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/5.jpg)
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;}
![Page 6: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/6.jpg)
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 ?
![Page 7: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/7.jpg)
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;}
![Page 8: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/8.jpg)
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;}
![Page 9: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/9.jpg)
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;}
![Page 10: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/10.jpg)
X
results
y
Call by value: “Sending a copy”
10DoubleIt
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
?
![Page 11: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/11.jpg)
X
results
y
Call by value: “Sending a copy”
5
?
DoubleIt 10
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;}
![Page 12: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/12.jpg)
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;}
![Page 13: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/13.jpg)
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;}
![Page 14: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/14.jpg)
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;}
![Page 15: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/15.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 16: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/16.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 17: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/17.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 18: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/18.jpg)
results ?
?TripleIt
Call by reference: “Getting the original”
int TripleIt (int&); // Prototypeint 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
![Page 19: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/19.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 20: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/20.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 21: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/21.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 22: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/22.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 23: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/23.jpg)
int TripleIt (int&); // Prototypeint 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
![Page 24: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/24.jpg)
results
y, x
?
TripleIt 21
14
Call by reference: “Getting the original”
int TripleIt (int&); // Prototypeint 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
_
![Page 25: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/25.jpg)
results 21
TripleIt 21
Call by reference: “Getting the original”
int TripleIt (int&); // Prototypeint 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
![Page 26: Functions ----------- Pass by Value Pass by Reference IC 210](https://reader035.vdocuments.net/reader035/viewer/2022062407/56649d615503460f94a42d1f/html5/thumbnails/26.jpg)
results 21
TripleIt ?
Call by reference: “Getting the original”
int TripleIt (int&); // Prototypeint 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