Exforsys.com
 

Sponsored Links

 

C++ Tutorials

 
Home Tutorials C++
 

C++ Inline Functions

 

C++ Inline Functions

In this C++ tutorial, you will learn about Inline function, what is inline function, reason for the need of inline function, what happens when an inline function is written, general format of inline function explained with example.


What is Inline Function?

Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program.


Reason for the need of Inline Function:

Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function saved program space and memory space are used because the function is stored only in one place and is only executed when it is called. This concept of function execution may be time consuming since the registers and other processes must be saved before the function gets called.


The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering “why not write the short code repeatedly inside the program wherever needed instead of going for inline function?” Although this could accomplish the task, the problem lies in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the concept of functions.


What happens when an inline function is written?

The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.


General Format of inline Function:

The general format of inline function is as follows:


inline datatype function_name(arguments)


The keyword inline specified in the above example, designates the function as inline function. For example, if a programmer wishes to have a function named exforsys with return value as integer and with no arguments as inline it is written as follows:


inline int exforsys( )


Example:


The concept of inline functions:



#include <iostream.h>
int exforsys(int);
void main( )
{
int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
cout<<”\n The Output is: “ << exforsys(x);
}


inline int exforsys(int x1)
{
return 5*x1;
}



The output of the above program is:


Enter the Input Value: 10
The Output is: 50


The output would be the same even when the inline function is written solely as a function. The concept, however, is different. When the program is compiled, the code present in the inline function exforsys( ) is replaced in the place of function call in the calling program. The concept of inline function is used in this example because the function is a small line of code.


The above example, when compiled, would have the structure as follows:



#include <iostream.h>
int exforsys(int);
void main( )
{
int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
//The exforsys(x) gets replaced with code return 5*x1;
cout<<”\n The Output is: “ << exforsys(x);
}



When the above program is written as normal function the compiled code would look like below:



#include <iostream.h>
int exforsys(int);
void main( )
{
int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
//Call is made to the function exforsys
cout<<”\n The Output is: “ << exforsys(x); 
}                          


int exforsys(int x1)
{
return 5*x1;
}




A programmer must make wise choices when to use inline functions. Inline functions will save time and are useful if the function is very small. If the function is large, use of inline functions must be avoided.



Read Next: Scope of Variables in Function



 

 

Comments


ruban_stalin said:

  very nice explanation
November 24, 2006, 12:34 am

rama@symphony said:

  if we delare a function as inline there is no guarantee always it will have inline behaviour. for example if the inline function has a recursion then the funtion will be no more inline. inline is just compiler directive. if inline function has a recursion then the behaviour of expanding the function will not be there.
September 30, 2007, 3:27 am

rb18 said:

  Definitions of inlinemethods and functions must be available in every
source file in which they are called. That makes sense if you think about it: how can the compiler substi-
tute the function body if it can’t see the function definition? Thus, if you write inlinefunctions or
methods you should place the definitions in a header file along with their prototypes. For methods, this
means placing the definitions in the .hfile that includes the class definition. This placement is perfectly
safe: the linker doesn’t complain about multiple definitions of the same method. It’s just like a #define
macro in this sense.
February 8, 2008, 5:15 pm

syed numan shah said:

  well i have not read the web very well,but it satisfies me too much.every thing is explained very simply and breifly.can we ask about the codes of some program.is there any software which can give us the codes of programs in c++
November 5, 2008, 7:29 am

Mahesh Tyagi said:

  *Inline functions here are explained here very briefly and nicely.Got the concept of the inline functions very clearly in the very first reading.
January 23, 2009, 2:33 am

1 said:

  very nice explanation for inline function.
January 25, 2009, 10:30 pm

jfl said:

  good and simple !
February 10, 2009, 4:56 am

Tsoomoo said:

  But still I didn't get Why any loop operation should not be used in inline function.
Any further explanation, please.
Thanks.
March 27, 2009, 7:09 am

M Sohail khan said:

  Without inline function our program can be executed so why we use inline functions.
June 3, 2009, 2:14 am

student said:

  When we declare inline for many times in a program for each and very function where will the memory be saved.
November 5, 2009, 2:25 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape