{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture6%2Dpost

# lecture6%2Dpost - Monday,October25 th FunctionReview...

This preview shows pages 1–9. Sign up to view the full content.

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

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

View Full Document
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)
Function Review int square(int x); // prototype int main(void) { int n; cout << "Enter a #: "; cin >> n; cout << square( n+1 ) << endl; return(0); } // function definition 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…

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

View Full Document
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 string nickname( int yourage ) { 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. strings! 3. A function may have more  than one return  statement.
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

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

View Full Document
// 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? Question : If so, what will it  print? Question : What type of data  does the function  barf   return ? Question : What type of  parameters  does  barf  have?
// prototype: void barf(void); int main(void) { int n = 10; barf(); cout << n; } // definition void barf(void) { int n; // local n = 50; cout << n << endl; }   Question : What will it print? 10 n 10 10 n 50 50 10 Function Review

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

View Full Document
// prototype: void barf(void); int main(void) { int x = 10; barf(); barf(); } // definition void barf(void) { int n; // local cout << n; n = 50; }   Function Review What will this program print???
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 32

lecture6%2Dpost - Monday,October25 th FunctionReview...

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

View Full Document
Ask a homework question - tutors are online