L5 - Function
L5 - Function
Function
1
Ahmed Farhan Ahnaf Siddique
Assistant Professor,
Department of Civil Engineering,
BUET
2
Function
• A function is a named block of code
int main() {}
4
User Defined Function
#include <iostream>
using namespace std;
int cube(int x)
{ // returns cube of x:
return x*x*x;
}
int main()
{ // tests the cube() function:
int n=1;
while (n != 0)
{cin >> n;
cout << "\tcube(" << n << ") = " << cube(n) << endl;
}
} 5
User Defined Function
• A user-defined function has two parts: its head and
its body; The syntax for the head is,
return-type name(parameter-list)
7
Function
8
Function
• The declaration tells the compiler the name, return
type and parameters of the function
10
Function
11
Function
• The variables that are listed in the function’s
parameter list are called parameters
14
Function Example - 2
#include <iostream>
using namespace std;
int circle();
int rectangle();
int main()
{
int option;
cout<<" Program to calculate area \n"<<" 1-Circle
\n"<<" 2-Rectangle \n\n";
cout<<" What option = ? ";
cin>>option;
if(option==1)circle();
if(option==2)rectangle();
} 15
Function Example - 2
int circle()
{
double radius;
cout<<"\n Radius = ? ";
cin>>radius;
cout<<"\n Area = "<<3.14159*radius*radius<<endl
<<endl;
}
int rectangle()
{
double length, width;
cout<<"\n Length = ? ";
cin>>length;
cout<<" Width = ? ";
cin>>width;
cout<<"\n Area = "<<length*width<<endl<<endl;
} 16
Function Example - 3
#include <iostream>
using namespace std;
double circle(double radius);
double rectangle(double length, double radius);
int main()
{
int option;
cout<<" Program to calculate area \n"<<" 1-Circle \n"<<" 2 -
Rectangle \n\n";
cout<<" What option = ? ";
cin>>option;
if(option==1)
{
double radius;
cout<<"\n Radius = ? ";
cin>>radius;
cout<<"\n Area = "<<circle(radius)<<endl<<endl;
17
}
Function Example - 3
if(option==2)
{
double length,width;
cout<<"\n Length = ? ";
cin>>length;
cout<<" Width = ? ";
cin>>width;
cout<<"\n Area = "<<rectangle(length, width) <<endl<<endl;
}
}
double circle(double radius)
{
return 3.14159*radius*radius;
}
double rectangle(double length, double width)
{
return length*width;
18
}
Pass By Value and Pass By Reference
• Pass by value means that the expression used in the
function call is evaluated first and then the
resulting value is assigned to the corresponding
parameter in the function’s parameter list before
the function begins executing
20
Pass By Value and Pass By Reference
21
Pass By Value
#include <iostream>
using namespace std;
int swap(int n1, int n2);
int main()
{
int x,y;
cout<<"\n x = ? "; cin>>x;
cout<<"\n y = ? "; cin>>y;
cout<<"\n\n main - after input : x = "<<x<<"\ty =
"<<y;
swap(x, y); 22
Pass By Value
cout<<"\n\n main - after calling swap : x =
"<<x<<"\ty = "<<y<<endl<<endl;
}
int swap(int x, int y)
{
int z;
z=x; x=y; y=z;
cout<<"\n\n swap - after interchange : x =
"<<x<<"\ty = "<<y;
return 0;
} 23
Pass By Value
24
Pass By Reference
#include <iostream>
using namespace std;
int swap(int& n1, int& n2);
int main()
{
int x,y;
cout<<"\n x = ? "; cin>>x;
cout<<"\n y = ? "; cin>>y;
cout<<"\n\n main - after input : x = "<<x<<"\ty =
"<<y;
swap(x, y); 25
Pass By Reference
cout<<"\n\n main - after calling swap : x =
"<<x<<"\ty = "<<y<<endl<<endl;
}
int swap(int& x, int& y){
int z;
z=x; x=y ; y=z;
cout<<"\n\n swap - after interchange : x =
"<<x<<"\ty = "<<y;
return 0;
}
26
Pass By Reference
27
The inline Function
Inline functions in C++ are a performance optimization technique that suggests to the compiler to substitute the
function's code directly at the point of call, rather than generating a separate function call instruction.
• When a function is called, it takes a lot of extra time
in executing a series of instructions for tasks such as
jumping to the function, saving registrars, pushing
arguments into the stack and returning to the
calling function
28
The inline Function
inline function-header
{
inline int add(int a, int b) {
return a + b;
function body }
29
Function Overloading/Polymorphism
• C++ allows you to use the same name for different
functions
30
Function Overloading/Polymorphism
#include <iostream>
using namespace std;
double x(int);
double x(double);
double x(int, int);
double x(int, double);
double x(double, int);
int main( )
{
cout<<"\n\n x(int 2) called-returned "<<x(2);
cout<<"\n\n x(double 2.0) called-returned "<<x(2.0);
cout<<"\n\n x(int 2, int 3) called-returned "<<x(2, 3);
cout<<"\n\n x(int 2, double 3.0) called-returned "<<x(2, 3.0);
cout<<"\n\n x(double 2.0, int 3) called-returned "<<x(2.0,3)
<<"\n\n";
} 31
Function Overloading/Polymorphism
double x(int y)
{return y;}
double x(double y)
{return y+1;}
double x(int y, int z)
{return y+z;}
double x(int y, double z)
{return y+z+1;}
double x(double y, int z)
{return y+z+2;}
32
Function Overloading/Polymorphism
33
Thank You!
34