Download presentation
Presentation is loading. Please wait.
Published byAda McLaughlin Modified over 9 years ago
1
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
2
The Hashemite University2 Outline Introduction. Functions with empty parameter lists. Inline functions. Functions with default arguments. Functions overloading. Functions templates. Examples.
3
The Hashemite University3 Functions with Empty Parameter Lists Empty parameter lists Either writing void or 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 Calling a function that does not return any value inside cout statement is syntax error (cout<<print() syntax error) Passing parameter to a function that does not take any parameter is syntax error (print (5), or print (x) syntax error)
4
The Hashemite University4 1// Fig. 3.18: fig03_18.cpp 2// Functions that take no arguments 3#include 4 5using std::cout; 6using std::endl; 7 8void function1(); 9void function2( void ); 10 11int main() 12{ 13 function1(); 14 function2(); 15 16 return 0; 17} 18 19void function1() 20{ 21 cout << "function1 takes no arguments" << endl; 22} 23 24void function2( void ) 25{ 26 cout << "function2 also takes no arguments" << endl; 27} function1 takes no arguments function2 also takes no arguments Notice the two ways of declaring no arguments.
5
The Hashemite University5 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; }
6
The Hashemite University6
7
7 Default Arguments I In the function prototype give all or some of the arguments default values. When you call the function, you can omit one or more of the arguments values. The omitted arguments will take their values from the default values in the function prototype. If function parameter omitted, gets default value Can be constants, global variables, or function calls If not enough parameters specified (in the calling statement), rightmost go to their defaults Default arguments in the prototype 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 );
8
The Hashemite University8 Default Arguments II In a function call you can omit the parameters that have default values only. Not setting 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 This means that no argument can take a default value unless the one on its right has a default value
9
The Hashemite University9 1// Fig. 3.23: fig03_23.cpp 2// Using default arguments 3#include 4 5using std::cout; 6using std::endl; 7 8int boxVolume( int length = 1, int width = 1, int height = 1 ); 9 10int 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 25int boxVolume( int length, int width, int height ) 26{ 27 return length * width * height; 28}
10
The Hashemite University10 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.
11
The Hashemite University11 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) Goal: having functions with the same name but they do different tasks Most of the time overloaded functions perform similar tasks ( i.e., a function to square int s, and function to square float s). int square( int x) {return x * x;} float square(float x) { return x * x; } Program chooses function by signature signature is determined by function name and its parameters Can have the same return types
12
The Hashemite University12 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.
13
The Hashemite University13 Function overloading III #include using namespace std; int fun1(int,float,char ); int main() { cout<<(5,3.3,'a'); return 0; } int fun1(int x1,float y1,char z1) { return x1+y1+z1; } int fun1(int x,float y,char z) { return x+y+z; } #include using namespace std; int fun1(int,float,char ); float fun1(int, float, char); int main() { cout<<(5,3.3,'a'); return 0; } int fun1(int x1,float y1,char z1) { return x1+y1+z1; } float fun1(int x,float y,char z) { return x+y+z; } #include using namespace std; int fun1(int,float,char ); int fun1(float, int, char); int main() { cout<<(5,3.3,'a'); return 0; } int fun1(int x1,float y1,char z1) { return x1+y1+z1; } int fun1(float y, int x,char z) { return x+y+z; } #include using namespace std; int fun1(int,float,char ); int fun1(int, float); int main() { cout<<(5,3.3,'a'); return 0; } int fun1(int x1,float y1,char z1) { return x1+y1+z1; } int fun1(int x,float y) { return x+y+z; } Syntax error. Two functions are the same Syntax error. changing return type is not enough Parameters order is different so it is ok # of parameters is different so it is ok
14
The Hashemite University14 1// Fig. 3.25: fig03_25.cpp 2// Using overloaded functions 3#include 4 5using std::cout; 6using std::endl; 7 8int square( int x ) { return x * x; } 9 10double square( double y ) { return y * y; } 11 12int 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 Functions have same name but different parameters
15
The Hashemite University15 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 // or template T square( T value1 ) { return value1 * value1; }
16
The Hashemite University16 Function Templates II T replaced by type parameter in function call. int x; int y = square(x); If int, all T 's become int s 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.
17
The Hashemite University17 Example // Fig. 3.27: fig03_27.cpp, Using a function template. #include using namespace std; // definition of function template maximum template // or template 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
18
The Hashemite University18 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 );
19
The Hashemite University19 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
20
The Hashemite University20 Output for the previous code
21
The Hashemite University21 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.