Chapter 2 Functions
Chapter 2 Functions
Inline functions
Friend functions
Overloaded functions
1-1 1
Function Parameters and arguments
argument.
As a result, if the function makes any changes to the
1-2 2
Example:
// function example
#include <iostream>
using namespace std;
int addition (int a, int b)
{
int r;
r=a+b;
return r;
}
int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
1-3 3
Passing by Reference
A reference parameter receives the address of the
argument
The variable itself is passed to the function
affects argument.
In passing by reference, in function declaration the type of
1-4 4
Example: pass by reference
Example:
int doubleValue(int x)
{
int value = x * 2;
return value ; // A copy of value will be returned here
} // value goes out of scope here
1-6 6
Return by value…
When to use return by value:
To return variables declared inside the function
To return arguments that were passed by value
When not to use return by value:
1-7 7
Return by Reference
include <iostream>
using namespace std;
• The return type of function test() is int& int n;
• The return statement is return n; it returns int& test();
variable n itself.
int main() {
• Then the variable n is assigned to the left
test() = 5;
side of code test() = 5; and value of n is
displayed cout<<n;
• Similar to pass by reference, values return 0; }
returned by reference must be variables
int& test() {
• Return by reference is also fast, which can
return n;
be useful when returning structs and
classes. }
1-8 8
Consider the following example:
passed by reference
1-9 9
Global vs local variables
The scope of variables declared within a function or any
other inner block is only their own function or their own
block and can not be used outside of their block.
1 - 10 10
Scope Operator
Int num1;
Void fun1(int num1)
Inline Function
DEFINITION:
In C++,the functions that are not actually
called, rather their code is expanded in line at the point of
each invocation such functions are called as inline functions.
1 - 12
13
SYNTAX
prototype fun_name( ); //function declaration
main( )
{
--------------------------
fun_name( ); //function call
}
inline prototype fun_name( );
//function definition
{
-------
}
1 - 13
14
1 - 14
15
SOME IMPORTANT POINTS
1.Inline function process is similar to using a macro.
1 - 15
16
When to use ?
1 - 16
17
1 - 17
18
Program
#include <iostream>
using namespace std;
inline int sqr (int x) //defined function as inlined
{
int y;
y = x * x;
return y;
}
int main()
{
int a =3, b; // declaration of variables
b = sqr(a); //function call
cout <<b;
return 0;
}
1 - 19
20
Program
#include <iostream.h>
using namespace std;
inline int Max(int x, int y) //defined function as inlined
{
return (x > y)? x : y;
}
int main( ) // Main function for the program
{
cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0;
}
1 - 20
21
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
1 - 21
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
inline double cube( const double s ) {
return s * s * s; }
int main() {
double side;
for ( int k = 1; k < 4; k++ ) {
cout << "Enter the side length of your cube: ";
cin >> side;
cout << "Volume of cube with side "
<< side << " is " << cube( side ) << endl;
} // end for
return 0;
} // end function main
1 - 22
23
1 - 23
24
1 - 24
25
1 - 25
26
1 - 27
Function Overloading
Signatures
C++ supports
Function overloading
Operator overloading
1 - 28
Function Overloading
Two or more functions can have
the same name but different
parameters
Example:
float max(float a, float b)
int max(int a, int b)
{
{
if(a>=b)
if(a>=b)
return a;
return a;
else
else
return b;
return b;
}
}
1 - 29
Overloading Function Overloading Call resolution
1 - 30
Overloading Function Overloading Call resolution
1 - 33 33
Outline
// using friend function
#include <iostream>
using namespace std;
class Distance
{
private:
int meter;
public:
Distance(): meter(0){ }
friend int func(Distance); //friend function
};
int func(Distance d) //function definition
{
d.meter=5; //accessing private data from non-member function
return d.meter;
}
int main()
{
Distance D; //D is object type of Class
cout<<"Distace: "<<func(D);
return 0;
}
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Function Templates
Function templates
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;
}
T replaced by type parameter in function call
int x;
int y = square(x);
If int parameter, all T's become ints
Can use float, double, long...
1 - 35
1 Outline
2 // Using a function template
3 #include <iostream>
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 template < class T >
10 T maximum( T value1, T value2, T value3 )
11 {
12 T max = value1;
13
14 if ( value2 > max )
15 max = value2;
16
17 if ( value3 > max )
18 max = value3;
19
20 return max;
21 } // end function template maximum
22
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
23 int main() Outline
24 {
25 int int1, int2, int3;
26
27 cout << "Input three integer values: ";
28 cin >> int1 >> int2 >> int3;
29 cout << "The maximum integer value is: "
30 << maximum( int1, int2, int3 ); // int version
31
32 double double1, double2, double3;
33
34 cout << "\nInput three double values: ";
35 cin >> double1 >> double2 >> double3;
36 cout << "The maximum double value is: "
37 << maximum( double1, double2, double3 ); // double version
38
39 char char1, char2, char3;
40
41 cout << "\nInput three characters: ";
42 cin >> char1 >> char2 >> char3;
43 cout << "The maximum character value is: "
44 << maximum( char1, char2, char3 ) // char version
45 << endl;
46
47 return 0;
48 } // end function main
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Outline
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.