c++ programming lecture 13 functions – part v the hashemite university computer engineering...
DESCRIPTION
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 (coutTRANSCRIPT
C++ ProgrammingLecture 13
Functions – Part V
The Hashemite UniversityComputer Engineering
Department
(Adapted from the textbook slides)
The Hashemite University 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 void or leaving a parameter list empty
indicates that the function takes no argumentsvoid 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)
The Hashemite University 4
1 // Fig. 3.18: fig03_18.cpp2 // Functions that take no arguments3 #include <iostream>45 using std::cout;6 using std::endl;78 void function1();9 void function2( void );1011 int main()12 {13 function1();14 function2();1516 return 0;17 }1819 void function1()20 {21 cout << "function1 takes no arguments" << endl;22 }2324 void function2( void )25 {26 cout << "function2 also takes no arguments" << endl;27 }
function1 takes no argumentsfunction2 also takes no arguments
Notice the two ways of declaring 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
The Hashemite University 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 );
The Hashemite University 8
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 errorint defaultFunction( int x = 1, int y, int z); Syntax errorThis means that no argument can take a default value unless the one on its right has a default value
The Hashemite University 9
1 // Fig. 3.23: fig03_23.cpp2 // Using default arguments3 #include <iostream>45 using std::cout;6 using std::endl;78 int boxVolume( int length = 1, int width = 1, int height = 1 );910 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;2021 return 0;22 }2324 // 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 10
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 11
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 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 is determined by function name and its
parameters Can have the same return types
The Hashemite University 12
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 13
Function overloading III#include<iostream>using namespace std;
int fun1(int ,float ,char );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<iostream>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<iostream>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<iostream>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
The Hashemite University 14
1 // Fig. 3.25: fig03_25.cpp2 // Using overloaded functions3 #include <iostream>45 using std::cout;6 using std::endl;78 int square( int x ) { return x * x; }910 double square( double y ) { return y * y; }1112 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; 1718 return 0;19 }
The square of integer 7 is 49The square of double 7.5 is 56.25
Functions have same name but different parameters
The Hashemite University 15
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 16
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 17
Example// Fig. 3.27: fig03_27.cpp, Using a function template.#include <iostream>
using namespace std;
// definition of function template maximumtemplate < 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 18
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 19
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 20
Output for the previous code
The Hashemite University 21
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