functions in c++

Post on 21-May-2015

3.378 Views

Category:

Education

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Functions in C++

Presented by Sachin Sharma

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.

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;

}

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

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.

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;}

Output

121.228

1.25713

Thank You

top related