lecture6

lecture6 - Whileyouwaitpuzzle:

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

View Full Document Right Arrow Icon
While-you-wait-puzzle: Sinbad, his first mate, and his second mate are shipwrecked on an island  and lose their memory.  They want to figure out who is who.  This is what  they know: If sailor A is Sinbad, then B is the first mate. If sailor B is not Sinbad, then C is the first mate. If sailor A is the first mate, then B is the second mate. Identify sailors A, B and C?  A:2nd, B:S, C:1st
Background image of page 1

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

View Full DocumentRight Arrow Icon
Monday, October 25 th   Function Review More on Functions Passing Variables using “References” Function Overloading and Default Parameters  Global and Static Variables Detecting errors in functions
Background image of page 2
Function Review int square(int x); // int square(int); int main(void) { int n; cout << "Enter a #: "; cin >> n; cout << square( n ) << endl; return(0); } // function definition int square( int x ) { int y; // new each time its called y = x * x; return (y); }   1. The prototype  * .   2. The function         definition:   a. Func header. b. Formal      parameter(s) c. Return type d. Function body e. Return      statement(s) 3. The function         call:   f. Local variables a. Actual        parameter(s)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Function Review int square(int x); // prototype int main(void) { int n; cout << "Enter a #: "; cin >> n; cout << square( n+1 ) << endl; return(0); } int square( int x ) { int y; // new each time its called y = x * x; return (y); }   Things to note: 1. All variables/  parameters in a  function are visible  only to the function. 2. When you call a function,  the value of the  actual  parameters  are copied  into the  formal  parameters . 3 4 3. All local variables/  parameters go away  when the function  exits…
Background image of page 4
Function Review string nickname(int x); // prototype int main(void) { int age; cout << "Enter your age: "; cin >> age; cout << nickname(age) << endl; return(0); } // function definition { if (yourage < 30) return( “kiddo” ); else return( “old fogie” ); }   Things to note: 1. If a function has a  non-void return type,  it  must  return a value. 2. The value returned must  have the same type as the  return type. 3. A function may have more  than one return  statement. 
Background image of page 5

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

View Full DocumentRight Arrow Icon
Function Review void eat(int x); // prototype int main(void) { int prunes; cout << “How many prunes? "; cin >> prunes; eat(prunes); cout << “aaah\n”; } void eat( int num_prunes ) { int i; for (i=0;i<num_prunes;i++) { if (i == 1) return; cout << “phhhbbbbtttt\n”; } }   Things to note: 1. A return statement  immediately exits the  function,  no matter  what . 52  prunes 52 num_prunes 52          i 0 0 < 52? 1 How many prunes? 52 phhhbbbbtttt aaah
Background image of page 6
// prototype: void barf(void); int main(void) { int n = 10; barf(); cout << n; } // definition void barf(void) { cout << n; // OK? n = 50; cout << n << endl; }   Function Review Question : Is this a valid  program?
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 / 33

lecture6 - Whileyouwaitpuzzle:

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