2012Week03Lecture3

12 passing by reference example vectors may be specied

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: b. •  In order to affect the actual value of arguments a and b when using the swap func1on, we must pass pointers to the values. swap(&a, &b); •  This is called passing by reference because we are passing the address of the actual variable. 10 Pointers and Func1on Arguments (cont’d) void swap(int *px, int *py) { int temp; temp = *px; *px = *py; *py = temp; in caller (main): a: b: } in swap: px: py: 11 Why Pass by Reference? •  Some reasons why variables are passed by reference: –  Rather than returning a single value and assigning it to a variable, we can instead manipulate the variable contents directly by using its address. –  It is oeen more intui1ve...
View Full Document

This note was uploaded on 10/07/2012 for the course ENG 2sh4 taught by Professor Doyle during the Fall '12 term at McMaster University.

Ask a homework question - tutors are online