Inline Function In C++ With Examples

(Last Updated On: April 4, 2021)

Inline functions:

Inline Function: By using function the size of the program is reduced. It is because a defined function can be called at multiple places in a program by using only a single call statement. When the compiler compiles the program, it generates a special code at the function call to jump to the function definition. It also generates another special code in the function definition (usually at the end of a function definition) to transfer back the control to the calling function. Thus, extra execution time is spent during the execution of the program to jump the control from the calling function to function definition and then return back to the calling function.

The code of body of the function can be inserted at each place of the function call to save the jumping time during program execution. The inline function is used for this purpose.

The inline function is a special function. It is a user-defined function but in it the function prototype is omitted. The keyword “inline” is used in the declaratory of the function definition. The inline function is defined before the main() function.

The actual code of the body of the function is inserted at the place f each function call during the compilation. If the inline function is called ten times in the program then the same code of the function will be inserted at each place of the function call. In this case, multiple copies of the same function code are inserted in the program and thus making the size of the program larger. The inline function is used only if the size of the function is small.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

The following program example finds out the cube of a number using the inline function:

inline function

Example: write a program by defining an inline function “kg” to convert pounds into kilograms:

1pound= 0.453592

inline function



Example: write a program by defining an inline function “circle_area” that takes the radius of a circle and returns the calculated area of the circle:

Area of circle = R2  and value of  is 3.1417

inline function


Example: write a program by defining an inline function “power” to find and return the exponential power, i.e. baseexponent:

For example, if base =2 and exponent = 3 then the function should return 8

 

inline function

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply