Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
This article will introduce you to an important concept in programming domain,that is Inline Function in C++ and follow it up with a practical demonstration. Following Pointers will be covered in this article
Let us get started with this article on Inline function in C++
One of the major objectives of using functions in a program is to save 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 tasks such as jumping to the calling function. When a function is small, a substantial percentage of execution time may be spent in such overheads and sometimes maybe the time taken for jumping to the calling function will be greater than the time taken to execute that function.
One solution to this problem is to use macro definitions, commonly known as macros. Preprocessor macros are popular in C, but the major drawback with macros is that they are not really functions and therefore, the usual error checking process does not occur during compilation.
C++ has a different solution to this problem. To eliminate the time of calls to small functions, C++ proposes a new function called inline function. An inline function is a function that is expanded in line when it is invoked thus saving time. The compiler replaces the function call with the corresponding function code that reduces the overhead of function calls.
We should note that inlining is only a request to the compiler, not a command. The compiler can ignore and skip the request for inlining. The compiler may not perform inlining in the following circumstances:
Syntax:
For an inline function, declaration and definition must be done together.
inline function-header { function-body }
Example:
#include <iostream> using namespace std; inline int cube(int s) { return s*s*s; } inline int inc(int a) { return ++a; } int main() { int a = 11; cout << "The cube of 3 is: " << cube(3) << "n"; cout << "Incrementing a " << inc(a) << "n"; return 0; }
Output:
Explanation:
It is a simple example which shows two inline functions declared using the inline keyword as a prefix.
Moving on with this article on Inline function in C++
We can use Inline function as per our needs. Some useful recommendation are mentioned below-
Moving on with this article on Inline function in C++
Points to be remembered while using Inline functions
Moving on with this article on Inline function in C++
Moving on with this article on Inline function in C++
Compilation overhead of copying the function body everywhere in the code at the time of compilation which is negligible in the case of small programs, but it may make a big difference in large codebases.
Thus we have come to an end of this article on ‘Inline Function in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.