lecture-06

lecture-06 - Functions Monday, September 26, 2011 Terms...

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

View Full Document Right Arrow Icon
Functions 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
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
Background image of page 2
Different kinds of parameters Value parameters Reference parameters Result parameters Value-result parameters Read-only parameters Monday, September 26, 2011
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 6
Value parameters 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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
Reference parameters int x = 1; void main () { foo(x, x); print(x); } y = 2; z = 3; print(x); } Monday, September 26, 2011
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

lecture-06 - Functions Monday, September 26, 2011 Terms...

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

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