0% found this document useful (0 votes)
3 views

Inline Function Cpp

Uploaded by

zeeshansari.390
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Inline Function Cpp

Uploaded by

zeeshansari.390
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Functions and

Constructors
Unit - II
2.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.
• In C++, we can declare a function as inline.
• This copies the function to the location of the function call in compile-
time and may make the program execution faster.
Syntax:

• inline return-type function-name(parameters)


•{
• // function code
•}
• When an instruction of a function call is encountered during the
compilation of a program, its memory address is stored by the compiler.
• The function arguments are copied on the stack and after the execution of
the code, the control is transferred to the calling instruction.
• This process can sometimes cause overhead in function calls, especially if
the function is small and its execution time is less than the switching time.
• This issue is resolved by using the inline functions.
• These functions overcome the overhead and also make the program faster
by reducing the execution time of the program.
• In case of inline functions, the compiler does not go through the above
process of switching between the stack and calling function.
• Instead, it copies the definition of the inline function and the calling
instruction with that definition
The compiler may not perform
inlining in such circumstances
as:
• If a function contains a loop. (for, while and do-while)
• If a function contains static variables.
• If a function is recursive.
• If a function return type is other than void, and the return statement
doesn’t exist in a function body.
• If a function contains a switch or goto statement.

You might also like