by Dinesh Thakur Category: Functions

Whenever a function is invoked, a set of operations is performed which includes passing the control from the calling function to the called function, managing stack for arguments and return values, managing registers, etc. All these operations take much of compiler time and slow down the execution process. This overhead can be avoided by using macros in a program. However, macros are not considered as true functions, as they do not perform type checking. Another way to make function calls execute faster and also perform type checking is to make the function inline.

An inline function is a function whose code is copied in place of each function call. In other words, each call to inline function is replaced by its code. Inline functions can be declared by prefixing the keyword inline to the return type in the function prototype. An inline function 'requests' the compiler to replace its each and every call by the code in its body. That is, specifying a function as inline is just a request to the compiler and not a command. So, it does not change the behavior of a function. Moreover, the compiler mayor may not choose to replace each call by the body. In case, it does, the function becomes 'in line' with the rest of the source code.

The syntax for inline function declaration is

            inline return_type function_name (parameter_list);

A difference between a normal function call and inline function call is shown in Figure

                    Difference between a normal and inline function call

In Figure, when the compiler reads the statement prod (a, b), it transfers the control to the prod () function. However, in Figure, the prod ( ) function is declared as an inline function. As a result, when the compiler reads the statement prod (a, b), it replaces the function call with the definition of the prod ( ) function.

Inline functions are ideal for functions that are small in size and frequently used by the programs. This is because inline functions reduce the time consumption and overhead involved in function calls. However, they also significantly increase the memory size of the program because each occurrence of inline function call is replaced by its code, which in turn may adversely affect the readability of the program. Also, these functions restrict the portability of the program.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles