0% found this document useful (0 votes)
26 views9 pages

Inline Function

Inline functions in C++ reduce function call overhead by expanding the function code at the call site instead of generating a call. Inline functions may increase efficiency for small functions but the compiler is not required to inline them in all cases such as functions with loops, static variables, or recursive calls.

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Inline Function

Inline functions in C++ reduce function call overhead by expanding the function code at the call site instead of generating a call. Inline functions may increase efficiency for small functions but the compiler is not required to inline them in all cases such as functions with loops, static variables, or recursive calls.

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

INLINE FUNCTION

One of the objective of using inline functions in a program is to save some memory space, which
becomes appreciable when a function is likely to be called many times. However, every time a
function is called, it takes a lot of extra time in executing a series of instruction or tasks such as
jumping to the function, saving registers, percentage of execution time may be spent in such
overheads.

One solution to this problem is to use macro definitions, popularly known as macros. Preprocessor
macros are popular in C. The major drawback with macros is that thy are not really functions and
therefore, the usual error checking does not occur during compilation. C++ has a different solution to
this problem. To eliminate the cost of calls to small functions, C++ Proposes a new feature called
inline function.
1
INLINE FUNCTION
C++ provides inline functions to reduce the function call overhead. An 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 of the inline function call. This substitution
is performed by the C++ compiler at compile time. An inline function may increase efficiency if it
is small.
• The compiler may not perform inlining in such circumstances as:

1. If a function contains a loop. (for, while and do-while)

2. If a function contains static variables.

3. If a function is recursive.

4. If a function return type is other than void, and the return statement doesn’t exist in a function body.

5. If a function contains a switch or goto statement.


inline return-type function-name(parameters)
{
// function code
}

Example - 1

#include <iostream>
using namespace std;
inline int cube(int s) { return s * s * s; }
int main()
{
cout << "The cube of 3 is: " << cube(3) << "\n";
return 0;
}
3
PROGRAM FOR INLINE FUNCTION
INLINE FUNCTION AND CLASSES

It is also possible to define the inline function inside the class. In fact, all the functions defined inside the class are implicitly
inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare an inline function
in the class then just declare the function inside the class and define it outside the class using the inline keyword.

6
Example 2

7
WHY INLINE FUNCTIONS ARE USED?

• When the program executes the function call instruction, the CPU stores the memory address
of the instruction following the function call, copies the arguments of the function on the
stack, and finally transfers control to the specified function.
• The CPU then executes the function code, stores the function return value in a predefined
memory location/register, and returns control to the calling function.
• This can become overhead if the execution time of the function is less than the switching
time from the caller function to called function (callee).
• For functions that are large and/or perform complex tasks, the overhead of the function call is
usually insignificant compared to the amount of time the function takes to run. However, for
small, commonly-used functions, the time needed to make the function call is often a lot
more than the time needed to actually execute the function’s code. This overhead occurs for
small functions because the execution time of a small function is less than the switching
time.
8
DEFAULT ARGUMENT FUNCTION
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling
function doesn’t provide a value for the argument. In case any value is passed, the default value is overridden.

You might also like