lec04-functions_part2

lec04-functions_part2 - CSC 1111 Introduction to Computing...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Reference Parameters and Function Overloading
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 void foo( int n) { cout << n << endl; n++; } int main() { int x = 10; foo(x); cout << x; return 0 ; } 1 2 3 4 5 6 7 8 9 10 11 12 10 10 void foo( int &m ) { cout << m << endl; m++; } int main() { int x = 10; foo(x); cout << x; return 0 ; } 10 11 Pass parameters by value Pass parameters by reference n is not x . Only the value of x is copied to n . m is x . They both refer to the same memory location. n 10 x 10 m 10 x
Background image of page 2
3 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void foo( int &m ) { m++; } int main() { int x = 10, y = 4; foo(x); cout << x << endl; foo(y) ; cout << y << endl; return 0 ; } 10 11 4 5 & indicates that the parameter is passed by reference to this function. m is tied to x only during this function call. They are not related after the function call. m is tied to y during this function call.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Why Passing Arguments by References? Pass multiple values between the callers and the callees Performance Passing data by reference Avoid copying data by value e.g.: Digital image, records of a patients, etc.
Background image of page 4
5 Implementing Swap function (wrong version) void wrong_swap(int x, int y) { int tmp; tmp = x; x = y; y = tmp; } int main(void) { int a = 10, b = 20; wrong_swap(a, b); cout << a << " " << b << endl; } 10 20 a b Before calling the function wrong_swap() , there are only two variables a and b . x , y and tmp do not exist yet.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Implementing Swap function (wrong version) void wrong_swap(int x, int y) { int tmp; tmp = x; x = y; y = tmp; } int main(void) { int a = 10, b = 20; wrong_swap(a, b); cout << a << " " << b << endl; } 10 20 a b 10 20 x y Upon calling the function wrong_swap() , variables x and y are being allocated. Only the values of the variables are passed to the function
Background image of page 6
7 Implementing Swap function (wrong version) void wrong_swap(int x, int y) { int tmp; tmp = x; x = y; y = tmp; } int main(void) { int a = 10, b = 20; wrong_swap(a, b);
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

lec04-functions_part2 - CSC 1111 Introduction to Computing...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online