Inline-function in c++ .

In this session i have discussed inline function in C++ .


The main purpose of using function is to save the memory space . When you will call a function it take the lot of extra time for the execution like jumping function , saving register , pushing argument in stack and returning to calling function . When function is small . It is very heavy process .

The C++ purposes the new features called inline function . The inline function is a function that expend in a line when it is executed .

Some situation where inline function may not work as follows .

1-Function contains the static variable .

2-Function is recursive .

3-Function contains loop , switch , goto .


Inline function syntax

    inline data type function-name(argument-list)
    {
        function body
    }
    

Example : Write a program that demonstrate the inline function .

  #include<iostream>
  using namespace std; 
  inline int sum(int x,int y)
  {
        return(x+y);
  }
  int main()
  {  
       int x,y ,result=0;
       cout<<"Enter the value for x : ";
       cin>>x;
       cout<<"Enter the value for y : ";
       cin>>y;
       result=sum(x,y);
       cout<<"Sum :  "<<result;
       return 0;
  }
  
  
  OUTPUT : Enter the value for x : 20
           Enter the value for y : 10
           Sum : 30
    


Welcome to AndroidATPoint.

back image                     next image