Inline Function In C++ With Example

Inline function:

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 the 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 inline functions 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. inline is used only if the size of the function is small.


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

inline function

Example: write a program by defining a 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 a 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

Related Article:


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Back to top button