{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture-07

lecture-07 - Functions Monday Terms • foo is the caller...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

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: Functions Monday, October 18, 2010 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, October 18, 2010 Different kinds of parameters • Value parameters • Reference parameters • Result parameters • Value-result parameters • Read-only parameters Monday, October 18, 2010 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, October 18, 2010 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, October 18, 2010 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, October 18, 2010 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, October 18, 2010 Reference parameters • “Call-by-reference” • Optional in Pascal (use “var” keyword) and C++ (use “&”) • 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, October 18, 2010 Reference parameters int x = 1; void main () { foo(x, x); print(x); } void foo(int &y, int &z) { y = 2; z = 3; print(x); } Monday, October 18, 2010 Reference 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, October 18, 2010 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); } void foo(int &y, int &z) { y = 2; z = 3; print(x); } Monday, October 18, 2010 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” int& foo( ... ) compute return values, store in memory, return address of return value Monday, October 18, 2010 Result parameters...
View Full Document

{[ snackBarMessage ]}

Page1 / 38

lecture-07 - Functions Monday Terms • foo is the caller...

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

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