lecture-06-6up - Terms Functions void foo() cfw_ int a, b;...

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

View Full Document Right Arrow Icon
Functions Monday, September 26, 2011 Terms foo is the caller bar is the callee a, b are the actual parameters to bar x, y are the formal parameters of bar Shorthand: argument = actual parameter parameter = formal parameter void foo() { int a, b; ... bar(a, b); } void bar(int x, int y) { ... } Monday, September 26, 2011 Different kinds of parameters Value parameters Reference parameters Result parameters Value-result parameters Read-only parameters Monday, September 26, 2011 Value parameters “Call-by-value” Used in C, Java, default in C++ Passes the value of an argument to the function Makes a copy of argument when function is called Advantages? Disadvantages? Monday, September 26, 2011 Value parameters int x = 1; void main () { foo(x, x); print(x); } void foo(int y, int z) { y = 2; z = 3; print(x); } Monday, September 26, 2011 Value parameters What do the print statements print? int x = 1; void main () { foo(x, x); print(x); } void foo(int y, int z) { y = 2; z = 3; print(x); } Monday, September 26, 2011
Background image of page 1

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

View Full DocumentRight Arrow Icon
What do the print statements print? Answer: print(x); //prints 1 print(x); //prints 1 int x = 1; void main () { foo(x, x); print(x); } void foo(int y, int z) { y = 2; z = 3; print(x); } Monday, September 26, 2011 Reference parameters “Call-by-reference” Pass the address of the argument to the function If an argument is an expression, evaluate it, place it in memory and then pass the address of the memory location Advantages? Disadvantages? Monday, September 26, 2011 Reference parameters int x = 1; void main () { foo(x, x); print(x); } y = 2; z = 3; print(x); } Monday, September 26, 2011 Reference parameters What do the print statements print? int x = 1; void main () { foo(x, x); print(x); } y = 2; z = 3; print(x); } Monday, September 26, 2011 Reference parameters What do the print statements print? Answer: print(x); //prints 3 print(x); //prints 3 int x = 1; void main () { foo(x, x); print(x); } y = 2; z = 3; print(x); } Monday, September 26, 2011 Result parameters Return values of a function Some languages let you specify other parameters as result parameters – these are un-initialized at the beginning of the function Copied at the end of function into the arguments of the caller C++ supports “return references” .. ) compute return values, store in memory, return address of return value Monday, September 26, 2011
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/19/2012 for the course ECE 468 taught by Professor Test during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 7

lecture-06-6up - Terms Functions void foo() cfw_ int a, b;...

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

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