Hello friends, in this post today, we learn about inline function in c++ and when should we use inline function in our program. so lets start...
![]() |
inline function in c++ |
Inline function:-
The functions can be made inline by adding prefix inline keyword to the function definition.
An inline function is a function that is extended in line when it is invoked.
The complier replaces the function code with the corresponding when function call.
Inline function saves time of calling function, saving registers, pushing arguments onto the stack and coming back from function.
Preprocessor macros are popular in C, which has similar kind of advantages mentioned above.
The major drawback of macros is that they are not really functions.
Therefore, the usual error checking does not occur during execution of macros.
When should we use inline function
Most important point:-We should be careful while using inline function. If function has only a few lines of code and simple expressions then only it should be used.
Remember:- Inline is a request to the compiler not a command.
The benefits of speed of inline functions reduces as the function grows in size.
So the compiler may ignore the request in some situations.
Critical situations for inline function:
1) If a loop, a switch or a goto exists in function body.
2) If function is not returning any value.
3) If function contains static variables.
4) If function is recursive.
Example:
Output:-
Function call is replaced with expression so c = cube(10), becomes c=27*27*27 and b=square, becomes b= 24*24*24*at compile time.
I hope that whatever information I have given in this post today, you have liked it and you have understood it.so keep learning and wait for the next post that will help you to increase your knowledge with the something new information.
Thank you so much for reading. And take care about yourself and your family.
Comments
Post a Comment