Inline function in C/C++

Inline function in C/C++

In this post we will see the concept of inline function in C C++. When we call a function, a lot of time is spent in executing a series of instructions, for tasks such as jumping to the function, saving registers, pushing arguments into stack and returning to calling function. C++ proposes a solution of line function to this problem. Inline function makes a program runs faster because the overhead of a function call and return is eliminated.

However it makes program to take up more memory, because the statements that define inline function are reproduce at each point where the function gets a call.

An inline function is a function that expands inline when it gets invoke. We define inline function as :
inline function header
{
function body
}

For e.g.
inline int area(int a, int b)
{
return a*b;
}

Generally we make a function inline, when they are small enough to define in one or two lines.

In some cases compiler may ignore inline request. Following are some of the reasons :
1. For functions returning value, if loop, switch or goto statement exists.
2. For functions not returning a value, if a return statement exists.
3. If a function contains static values.
4. If inline function are recursive.

Now let’s see an example of inline function.

Q. Write a program in C/C++ to show use of inline function.

1. Program

C Code :

#include <stdio.h>
static inline int sum(int x, int y) {
  int ans = 0;
  while (x <= y) {
    ans += x;
    x++;
  }
  return ans;
}

int main() {
  printf(" Sum of all digits from 0 to 10 is: %d\n", sum(0, 10));
  printf(" Sum of all digits from 1 to 50 is: %d\n", sum(1, 50));
  printf(" Sum of all digits from -5 to 5 is: %d\n", sum(-5, 5));
  return 0;
}

Note: In C, Normally GCC’s file scope is not external linkage. That means inline function is never provided to the linker causing Compiler error: undefined reference to ‘sum’. To solve this ,use “static” before inline. It forces the compiler to consider the inline function, and program compiles and run successfully.

C++ Code :

#include <iostream>
using namespace std;
inline int sum(int x, int y) {
  int ans = 0;
  while (x <= y) {
    ans += x;
    x++;
  }
  return ans;
}

int main() {
  cout << " Sum of all digits from 0 to 10 is: " << sum(0, 10) << endl;
  cout << " Sum of all digits from 1 to 50 is: " << sum(1, 50) << endl;
  cout << " Sum of all digits from -5 to 5 is: " << sum(-5, 5) << endl;
  return 0;
}

2. Output-

inline function in C C++

3. Program Parts-

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. printf/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

Conclusion-

So this was the concept of Inline function in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

For daily updates do follow us on Instagram. Visit again!

ErrorFreeProgram.

2 COMMENTS

Leave a Reply

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