Breaking News

Inline Functions In C++

There may be instances when you want to repeat a process more than once in a program. Well, you may think of going for the concept of functions. It’s fine as long as your function is a bit long.
 
Suppose there are only one or two lines you want to repeat? Is it worthwhile going for a function?
 
When our compiler friend sees a function call, the compiler has to save the present memory address and then go to a new memory address to access the function. This means it will take some time and also use some memory space.
 
If a function is very small, then there is no need to go for branching to sub routines (i.e. there is no need for using another function).
 
Instead, we can make use of the inline functions. Inline functions will simply substitute the lines into the main program where the function is called.
 
Example:
 
 
Out put of the program
 
 
As you can see above, the inline function is very similar to a normal function. Only difference is that you have to mention the keyword inline before the function. When the compiler comes to the function call, it knows that mtocms is an inline function.
 
So the compiler will insert the body of the function in place of the function call. After making the substitutions wherever the inline function has been called (in our program it is in only one place), the compiler will execute the program.
 
The advantage is that the compiler doesn’t need to save its present memory address, go to the function’s memory address, execute the function and return back to the original address. Instead the code is brought into the main program.
 
Thus time consumption can be reduced.
 
 

Check Also

Insertion & Extraction In C++

Insertion is the operation of sending characters to a stream, expressed by the overloaded insertion …