What is an Inline function? Explain how to implement it

Estudies4you

Inline functions, Recursive functions

 Q41. What is an Inline function? Explain how to implement it.
Answer:

inline Function
Inline functions can be defined as functions which are expanded in line during its invocation. The‘declaration and definition of such functions can be done simultaneously. ‘Whenever, these functions are called, the compiler replaces the call with its respective function code. Generally, inline functions are used for executing small functions as it reduces the overhead incurred during the normal function call. Inlining means implicitly embedding the copy of the function body during its invocation.
The inline functions are declared = inline keyword which tells the compiler, to replace the function, call with the function code.
Syntax
inline func_name
{
statement 1;
statement 2; 
.................

}
Example
inline float power( float x)
{
return ( x*x);

}
However, there are certain situations where inline functions are not applicable. They are as follows,
  • Inline function cannot be applied to recursive function.
  • The functions Porn static variables cannot be used.
  • The functions containing statements like for-loop, if, switch, break and so on are also not applicable.
  • The inline function cannot use main( ) as inline.
Advantages
  1. It increases the speed of the program because the function is not called each time it is invoked instead a copy of it replaces the function call. :
  2. It is easier to read because the function contains only one or two statements
Disadvantages
  1. It increases the size of the executable program.
  2. It is difficult to read when the function code is large.
Program
#include<conio.h>
#include<iostream.h>
#define square(x) x*x
inline float sq(float f)
{
return(f*f);
}
int main()
{
clrscr( );
float a = 3.0, b = 3.0, c, d;
c = square(++a);
d=sq(++b);
cout<<“c="<<c<<"\n"<<"d="<<d;
getch( );
return 0;
}

Output
how to implement inline function in c++,What is an Inline function in c++,Default Arguments in c++,c++ lecture notes,c++ notes,c++ study material,c++ previous question papers,oops using c++ notes,oops using c++ lecture notes,oops using  c++ study material,oops through c++ notes,oops through c++ study material,oops through c++ lecture notes,c++ notes jntuh,c++ notes jntu,jntu c++ notes,

To Top