Functions 1. Example: Power, Square Root and Absolute values of a number #include … float num;...

52
Chapter 7-8 Functions 1

Transcript of Functions 1. Example: Power, Square Root and Absolute values of a number #include … float num;...

  • Slide 1
  • Functions 1
  • Slide 2 > num; power = pow ( num, 2 ); cout > num1 >> num2; m"> num2 ) result = num1; else result = num2; return result; } // function prototype (function declaration) // function heading function definition // function call 5">
  • Example: MAX of 2 numbers #include using namespace std; int Max ( int num1, int num2 ); int main() { int num1, num2, maxResult; cin >> num1 >> num2; maxResult = Max ( num1, num2 ); cout
  • Review: Element of a function int Max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } Return Data TypeFunction NameParameter List Function Body 6
  • Slide 7
  • Function Prototype C++ programmer usually place all supporting functions after the main function. C++ rule: identifiers must be declared before use! Place a function prototype before the main function as a declaration. Two ways to declare a function: int max ( int num1, int num2 ); OR int max ( int, int ); 7 Add a semicolon to the function heading, and you get the function prototype!
  • Slide 8
  • Function Prototype #include using namespace std; int max ( int num1, int num2 ); int main() { maxResult = max ( num1, num2 ) } int max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } #include using namespace std; int max ( int num1, int num2 ) { int result; if ( num1 > num2 ) result = num1; else result = num2; return result; } int main() { maxResult = max ( num1, num2 ) } = 8 Common Practice !
  • Slide 9
  • Review: Two Types of Functions Void Function Return data type is void Dont need a return statement in the function Function call format: functionName(parameters); Value-Return Function Return data type is some real data type ( int, float, string ) Need a return statement to return a value to the caller function Function call format: var = functionName(parameters); OR use functionName(parameters) as a variable. 9
  • Slide 10
  • Formal parameters vs. Actual parameters (Arguments) Formal parameters appears in a function heading. int max ( int num1, int num2 ); Used in the callee function. Actually parameters (Arguments) appear in a function call. result = max ( 2, 3 ); During a function call, an argument can be a literal, variable, or expression. result = pow( 3, 2 ); result = fabs ( num ); result = sqrt ( num / 2 + 1 ); A function is not required to have arguments. eof(); Used in the caller function. 10
  • Slide 11
  • Void Function #include using namespace std; const int HEIGHT = 10; void PrintLine(int height); int main() { int height = HEIGHT; while ( height > 0 ) { PrintLine(height); height --; } return 0; } void PrintLine(int height) { string spaceLine = ""; string dollarLine = ""; int count = 0; while ( count < height - 1 ) { spaceLine += " "; count ++; } count = 0; while ( count < 2 * ( HEIGHT - height ) + 1 ) { dollarLine += "$"; count ++; } cout > rate; cout > hours; salary = GrossPay(rate, hours); cout > num; alpha += num; } cout
  • Example: GrossPay #include using namespace std; float GrossPay(float payRate, float hour); int main() { int hour; float rate; float salary; cout > rate; cout > hour; salary = GrossPay(rate, hour); cout
  • 24 Example: Using a flag float DoIt(int num, char op); int main() { int base; float result; char choice; cout > base; cout > choice; while (choice != C && choice != S) { cout > choice; } result = DoIt(base, choice); cout
  • 25 Example: Using a flag int DoIt(int num, char op); int main() { int base; float result; char choice; cout > base; cout > choice; while (choice != C && choice != S) { cout > choice; } result = DoIt(base, choice); cout