Presentation is loading. Please wait.

Presentation is loading. Please wait.

Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function.

Similar presentations


Presentation on theme: "Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function."— Presentation transcript:

1 Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function Definition * Examples for calling Functions * Common Programming Errors - Exercise/How Work Dr. Ming Zhang

2 Mathematical Library Functions Function Name & Argument(s) Description int abs(int i) Absolute value of i double fabs(double d) Absolute value of d double pow(double d1, double d2) d1 raised to d2 power double exp(double d) e raised to d power double sqrt(double d) square root of d double sin(double d) Sine of d(d in radians) double cos(double d) Cosine of d(d: radians) double log(double d) Natural log of d double log10(double d) Common log of d Completing the Basic Dr. Ming Zhang

3 Mathematical Library Functions Examples Example Returned Value abs( -3) 3 fabs(-7.362) 7.362000 pow(2.0, 5.0) 32.000000 exp(-3.2)0.040762 sqrt(16.0)4.000000 sin(0.0) 0.000000 cos(0.0)1.000000 log(18.697)2.928363 log10(18.697) 1.271772 Completing the Basic Dr. Ming Zhang

4 Program Components in C++ *Models in C++ - Functions - Classes * Functions - Pre-packaged function C++ standard library functions - Programmer-defined functions The programmer writes Dr. Ming Zhang

5 Function Call * Function Call The function call specifies the function name and provides information (as arguments) that the called function needs to do its job. And the called function returns the result to calling function (caller) * Example …… double time, height; height = 800.0; time = sqrt( 2.0 * height/32.2 ); …… Dr. Ming Zhang

6 Function Relationship main( ) function1 function2 function3 function11 function12 function31 function111 function112 function311 Dr. Ming Zhang

7 Motivations of Functionalizing a Program * More Manageable The divide-and-conquer approach makes program development more manageable. * Software Reusability Using existing functions as building blocks to create new programs. * Avoiding Repeating Code in a Program Packaging code as a function allows the code to be executed from several locations in a program simple by calling the function. Dr. Ming Zhang

8 Function Prototype * Function Prototype return-value-type function-name( data-type list) * Example of Function Prototype int square (int ) * The function prototype is not required if the definition of the function appears before the function’s first use in the program. In such case, the function definition also acts as the function prototype. Dr. Ming Zhang

9 Format of Function Definition * Format of Function Definition return-value-type function-name (parameter-list) { declarations statements } * Example int square (int y) { return y*y; } Dr. Ming Zhang

10 Three Returns of a Function * Control is returned simple when the function-ending right brace is reached. Function does not return a result. * Executing the statement return; Function does not return a result. * If function does return a result, the statement return expression; returns the value of expression to the caller. Dr. Ming Zhang

11 Example of Using Function (Fig. 3.3) # include using std::cout; int square ( int); // function prototype int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) // Function definition { return y*y; } Dr. Ming Zhang

12 Example of Using Function (Fig. 3.4) …… int maximum ( int, int, int); //function prototype ….. cout << maximum (a, b, c); // call function …… int maximum (int x, int y, int z) { int max =x; if (y> max) max =y; if (z > max) max = z; return max } Dr. Ming Zhang

13 Common Error 1 -Exercise/Home work # include using std::cout; int square ( int); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } void square( int y) { return y*y; } -------------------------ERROR------------------------------ Dr. Ming Zhang

14 Common Error 2 -Exercise/Home work int maximum ( int, int, int); //function prototype ….. cout << maximum (a, b, c); // calling function …… int maximum (int x, y, z) { int max =x; if (y> max) max =y; if (z > max) max = z; return max } -------------------------ERROR------------------------------ Dr. Ming Zhang

15 Common Error 3 -Exercise/Home work # include using std::cout; int square ( int); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y); { return y*y; } -------------------------ERROR------------------------------ Dr. Ming Zhang

16 Common Error 4 -Exercise/Home work # include using std::cout; int square ( int); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { int y; return y*y; } -------------------------ERROR------------------------------ Dr. Ming Zhang

17 Common Error 5 -Exercise/Home work # include using std::cout; void printName( ); int main ( ) { for ( int x =1; x <=10; x++) cout << printName<<endl; return(0); } int printName( ) { cout << “Ming Zhang” << endl; return; } -------------------------ERROR------------------------------ Dr. Ming Zhang

18 Common Error 6 -Exercise/Home work # include using std::cout; using std::endl; int square ( int); void printName( ); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { void printName( ) { cout << “Ming Zhang”<< endl;} return y*y; } -------------------------ERROR------------------------------ Dr. Ming Zhang

19 Common Error 7 -Exercise/Home work # include using std::cout; int square ( float); int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { return y*y; } -------------------------ERROR------------------------------ Dr. Ming Zhang

20 Common Error 8 -Exercise/Home work # include using std::cout; int square ( int) int main ( ) { for ( int x =1; x <=10; x++) cout << square(x) << “ “; return(0); } int square( int y) { return y*y; } -------------------------ERROR------------------------------ Dr. Ming Zhang


Download ppt "Function (L16) * Mathematical Library Functions * Program Components in C++ * Motivations for Functionalizing a Program * Function Prototype * Function."

Similar presentations


Ads by Google