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
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
Mathematical Library Functions Examples Example Returned Value abs( -3) 3 fabs(-7.362) pow(2.0, 5.0) exp(-3.2) sqrt(16.0) sin(0.0) cos(0.0) log(18.697) log10(18.697) Completing the Basic Dr. Ming Zhang
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
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
Function Relationship main( ) function1 function2 function3 function11 function12 function31 function111 function112 function311 Dr. Ming Zhang
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
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
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
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
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
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
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
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
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
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
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
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
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
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