lecture6%2Dpost

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

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

View Full Document Right 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 1

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); // 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 2
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…
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 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. 
Background image of page 4
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 5

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

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

This note was uploaded on 04/10/2012 for the course CS 31 taught by Professor Melkanoff during the Winter '00 term at UCLA.

Page1 / 32

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

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

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