1 / 18

C++ Programming Lecture 13 Functions – Part V

This lecture covers functions with empty parameter lists, inline functions, functions with default arguments, function overloading, and functions templates.

bwilcox
Download Presentation

C++ Programming Lecture 13 Functions – Part V

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ ProgrammingLecture 13Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

  2. Outline • Introduction. • Functions with empty parameter lists. • Inline functions. • Functions with default arguments. • Functions overloading. • Functions templates. • Examples. The Hashemite University

  3. Functions with Empty Parameter Lists • Empty parameter lists • Either writing voidor leaving a parameter list empty indicates that the function takes no arguments void print(); or void print( void ); • Function print takes no arguments and returns no value The Hashemite University

  4. 1 // Fig. 3.18: fig03_18.cpp 2 // Functions that take no arguments Notice the two ways of declaring no arguments. 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 void function1(); 9 void function2( void ); 10 11 int main() 12 { 13 function1(); 14 function2(); 15 16 return 0; 17 } 18 19 void function1() 20 { 21 cout << "function1 takes no arguments" << endl; 22 } 23 24 void function2( void ) 25 { 26 cout << "function2 also takes no arguments" << endl; 27 } function1 takes no arguments function2 also takes no arguments The Hashemite University

  5. Inline Functions • inline functions • Reduce function-call overhead • Asks the compiler to copy code into program instead of using a function call • Compiler can ignore inline • Should be used with small, often-used functions • Example: inline double cube( const double s ) { return s * s * s; } The Hashemite University

  6. Default Arguments I • If function parameter omitted, gets default value • Can be constants, global variables, or function calls • If not enough parameters specified, rightmost go to their defaults • Must be the right most arguments or parameters for a function. • Can be used with inline functions also. • Set defaults in function prototype (only) where the variables names are provided just for readability. int defaultFunction( int x = 1, int y = 2, int z = 3 ); The Hashemite University

  7. Default Arguments II • In a function call you can omit the parameters that have default values only. • Not setting the all the rightmost parameters after a default arguments to default is a syntax error. • E.g.: int defaultFunction(int x = 1, int y, int z = 3);  Syntax error int defaultFunction( int x = 1, int y, int z);  Syntax error The Hashemite University

  8. 1 // Fig. 3.23: fig03_23.cpp 2 // Using default arguments 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int boxVolume( int length = 1, int width = 1, int height = 1 ); 9 10 int main() 11 { 12 cout << "The default box volume is: " << boxVolume() 13 << "\n\nThe volume of a box with length 10,\n" 14 << "width 1 and height 1 is: " << boxVolume( 10 ) 15 << "\n\nThe volume of a box with length 10,\n" 16 << "width 5 and height 1 is: " << boxVolume( 10, 5 ) 17 << "\n\nThe volume of a box with length 10,\n" 18 << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) 19 << endl; 20 21 return 0; 22 } 23 24 // Calculate the volume of a box 25 int boxVolume( int length, int width, int height ) 26 { 27 return length * width * height; 28 } The Hashemite University

  9. Program Output The default box volume is: 1 The volume of a box with length 10, width 1 and height 1 is: 10 The volume of a box with length 10, width 5 and height 1 is: 50 The volume of a box with length 10, width 5 and height 2 is: 100 Notice how the rightmost values are defaulted. The Hashemite University

  10. Function Overloading I • Function overloading means Having functions with same name and different parameters (different number of parameters, or different data types, or different order, or all of these issues at the same time) • Most of the time overloaded functions perform similar tasks ( i.e., a function to square ints, and function to square floats). int square( int x) {return x * x;} float square(float x) { return x * x; } • Program chooses function by signature • signature determined by function name and parameter types • Can have the same return types The Hashemite University

  11. Function Overloading II • You cannot overload a function with default arguments with another version that takes no arguments  syntax error. • Overloading a function with another version that have the same parameters numbers, types, and order with just the return result data type is different is a syntax error. • Operator overloading like >> and <<, also & will be taken in OOP course. The Hashemite University

  12. 1 // Fig. 3.25: fig03_25.cpp 2 // Using overloaded functions Functions have same name but different parameters 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 8 int square( int x ) { return x * x; } 9 10 double square( double y ) { return y * y; } 11 12 int main() 13 { 14 cout << "The square of integer 7 is " << square( 7 ) 15 << "\nThe square of double 7.5 is " << square( 7.5 ) 16 << endl; 17 18 return 0; 19 } The square of integer 7 is 49 The square of double 7.5 is 56.25 The Hashemite University

  13. Function Templates I • Function templates • Used only when the overloaded functions have the same logic (body) and same number of parameters (just the data type is different) • Compact way to make overloaded functions • Keyword template • Keyword class or typename before every formal type parameter (built in or user defined) template < class T > // or template< typename T > T square( T value1 ) { return value1 * value1; } The Hashemite University

  14. Function Templates II • T replaced by type parameter in function call. int x; int y = square(x); • If int, all T's become ints • Can use float, double, long... • You can use any character to replace T, e.g. class R. • You can specify the return data type for a template function (not always put it as T). • You can specify the data type of some parameters (not always put it as T). • Passing different values for T is syntax error. The Hashemite University

  15. Example // Fig. 3.27: fig03_27.cpp, Using a function template. #include <iostream> using namespace std; // definition of function template maximum template < class T > // or template< typename T > T maximum( T value1, T value2, T value3 ) { T max = value1; if ( value2 > max ) max = value2; if ( value3 > max ) max = value3; return max; } // end function template maximum The Hashemite University

  16. Example … cont. int main() { // demonstrate maximum with int values int int1, int2, int3; cout << "Input three integer values: "; cin >> int1 >> int2 >> int3; // invoke int version of maximum cout << "The maximum integer value is: “ << maximum( int1, int2, int3 ); // demonstrate maximum with double values double double1, double2, double3; cout << "\n\nInput three double values: "; cin >> double1 >> double2 >> double3; // invoke double version of maximum cout << "The maximum double value is: “ << maximum( double1, double2, double3 ); The Hashemite University

  17. Example … cont. // demonstrate maximum with char values char char1, char2, char3; cout << "\n\nInput three characters: "; cin >> char1 >> char2 >> char3; // invoke char version of maximum cout << "The maximum character value is: " << maximum( char1, char2, char3 ) << endl; return 0; // indicates successful termination } // end main The Hashemite University

  18. Additional Notes • This lecture covers the following material from the textbook: • Fourth Edition • Chapter 3: Sections 3.15, 3.16, 3.18, 3.20, 3.21 The Hashemite University

More Related