C++ Inline Functions

C++ Inline Functions

In this C++ tutorial, you will learn about Inline function, what is inline function, reason for the need of inline function, what happens when an inline function is written, general format of inline function explained with example.

What is Inline Function?

Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program.

Reason for the need of Inline Function:

Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function saved program space and memory space are used because the function is stored only in one place and is only executed when it is called. This concept of function execution may be time consuming since the registers and other processes must be saved before the function gets called.

The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering “why not write the short code repeatedly inside the program wherever needed instead of going for inline function?” Although this could accomplish the task, the problem lies in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the concept of functions.

What happens when an inline function is written?

The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.

General Format of inline Function:

The general format of inline function is as follows:

inline datatype function_name(arguments)

The keyword inline specified in the above example, designates the function as inline function. For example, if a programmer wishes to have a function named exforsys with return value as integer and with no arguments as inline it is written as follows:

inline int exforsys( )

Example:

The concept of inline functions:


#include <iostream.h>
int exforsys(int);
void main( )
{
int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
cout<<”\n The Output is: “ << exforsys(x);
}

inline int exforsys(int x1)
{
return 5*x1;
}


The output of the above program is:

Enter the Input Value: 10
The Output is: 50

The output would be the same even when the inline function is written solely as a function. The concept, however, is different. When the program is compiled, the code present in the inline function exforsys( ) is replaced in the place of function call in the calling program. The concept of inline function is used in this example because the function is a small line of code.

The above example, when compiled, would have the structure as follows:


#include <iostream.h>
int exforsys(int);
void main( )
{
int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
//The exforsys(x) gets replaced with code return 5*x1;
cout<<”\n The Output is: “ << exforsys(x);
}


When the above program is written as normal function the compiled code would look like below:


#include <iostream.h>
int exforsys(int);
void main( )
{
int x;
cout << “\n Enter the Input Value: ”;
cin>>x;
//Call is made to the function exforsys
cout<<”\n The Output is: “ << exforsys(x);
}

int exforsys(int x1)
{
return 5*x1;
}



Sponsored Links
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值