functions in c++

8
Functions in C++ Presented by Sachin Sharma

Upload: sachin-sharma

Post on 21-May-2015

3.389 views

Category:

Education


4 download

TRANSCRIPT

Page 1: Functions in C++

Functions in C++

Presented by Sachin Sharma

Page 2: Functions in C++

Function Overloading

• C++ enables several functions of the same name to be defined, as long as these functions have different sets of parameters (at least as far as their types are concerned). This capability is called function overloading.

Page 3: Functions in C++

Example To Use The concept of function overloading

# include<iostream.h># include<conio.h>void area (float r);void area (float l, float b);void main(){

float r1, l1, b1;clrscr();

cout<<“ Enter value of r1: ”<<endl;cin>>r1;cout<<“ Enter value of l1: ”<<endl;cin>>l1;cout<<“ Enter value of b1: ”<<endl;cin>>b1;

cout<<“ Area of the circle is “<<endl;area (r1);cout<<“ Area of the rectangle is “<<endl;area (l1, b1);}void area (float r){

float a = 3.14*r*r;cout<<“ Area = “<<a<<endl;

}void area (){

float a1 = l*b;cout<<“Area = “<<a1<<endl;

}

Page 4: Functions in C++

OutputEnter value of r1:2Enter value of l1:4Enter value of b1:6Area of the circle isArea = 12.56Area of the rectangle isArea = 24

Page 5: Functions in C++

Inline Functions in C++• Whenever we call a function, control jump to

function and come back to caller program when execution of function is completed. This process takes a lot of time. C++ provides a solution of this problem, that is inline function. With inline function, the compiler replaces the function call statement with the function code itself (process called expansion) and then compiles the entire code. Thus, with inline functions, the compiler does not have to jump to another location to execute the function, and then jump back as the code of the called function is already available to the calling program.

Page 6: Functions in C++

Example to demonstrate inline functions

#include<iostream.h>#include<conio.h>inline float mul (float x, float y){

return (x*y);}inline double div (double p, double q){

return (p/q);}int main(){

float a = 12.345;float b = 9.82;clrscr();

cout<<mul (a,b)<<endl;cout<<div (a,b)<<endl;return 0;}

Page 7: Functions in C++

Output

121.228

1.25713

Page 8: Functions in C++

Thank You