on Leave a Comment

Inline function in C++

Inline function is a concept in C++ that helps in reducing execution time of small functions. As we have already know that a function utilizes memory space but increases execution time, because of some tasks like saving registers, pushing arguments into the stack etc.

If function is large, then it is useful because it saves more memory space, but it also increases execution time. But comparison to the advantage of memory space, execution time may appear negligible.  

But if function is small, it saves less memory space and increasing execution time.

Inline function is a way of solving problems of small function. Inline function is a function that is expanded in a line when it is invoked. Compiler replaces function call statement into corresponding function code.

Syntax of defining inline function:

inline <return-type> <function-name> (<list of arguments>)
{
                function body
}

Example:

inline int add(int a, int b)
{
                return a+b;
}

PROGRAM:

inline function in c++


OUTPUT:

C++ inline function program output


It is important know that inline keyword is a request not a command. A compiler can ignore this request if the following conditions satisfy;
  • Function containing loop, switch and goto statements.
  • Function is recursive.
  • Function containing static variables. 

0 comments:

Post a Comment