In-line Function in C++





What is inline functions?:

  • The inline functions are a C++ enhancement feature to increase the execution time of a program.

  • It reduces the function call overhead.

  • Inline function is a function that is expanded in line when it is called.

  • When the inline function is called whole code of the inline function gets inserted or substituted at the point on inline function call.

  • This substitution is performed by C++ compiler at compile time.

  • Inline function may increase efficiency if it is small.

  • To declare a function as inline use the keyword inline before the function name.

Syntax:

inline return-type function-name( parameter-list)
 {
   //function definition
 }                        
                    

Note:A function definition in a class definition is an inline function definition, even without the use of the inline specifier.

Example:

Below is an inline function that finds the cube of a number.

#include<iostream>
using namespace std;
inline int cube(int x)
 {
   return x*x*x;
 }
int main() {
  int n, res;
  cout<<"Enter Number :";
  cin>>n;
  res = cube(n);
  cout<<" CUBE of "<< n;
  cout<<" is :"<< res;
  cout<< endl;
  return 0; 
 }

Inline is only a request to the compiler, not a command.

Compiler may not perform inlining in such cases:--
  1. If a function contains a loop.
  2. If a function contains static variable.
  3. If a function is recursive.
  4. If a function contains switch or goto statement.

Inline Function Advantages:

  1. Function call overhead does not occur.
  2. It also saves the overhead of push/pop variable on the stack when function is called.
  3. It also saves overhead of return call from function.

Inline Function Disadvantages:

  1. If you use too many inline functions the the size of the binary executable file will be large, because of duplication of the same code.
  2. Inline function may increase compile time overhead.
  3. Too much inlining can reduce your instruction cache hit rate.

Next topic is Function Overloading





 



Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved