Understanding Inline Function in C++

Understanding Inline Function in C++

5 mins read1.7K Views Comment
Updated on Apr 4, 2023 12:30 IST

An inline function in C++ is a crucial feature that enhances the performance of a program by reducing the time it takes to execute repetitive function calls. With this feature, the code for the function is compiled into the calling program, eliminating the need to call the function repeatedly and reducing the overall execution time. This results in an improvement in the speed of the program’s execution.

2023_02_Understanding-Inline-Function-in-C.jpg

In this tutorial, we will discuss inline in C++ and understand its working through code examples. We will be covering the following sections:

Introduction to Inline Function in C++

An inline function in C++ is a function that is declared with the “inline” keyword. The main purpose of using inline functions is to improve the performance of a program by reducing the overhead of function calls.

When a program calls a function, there is a certain amount of overhead involved in setting up the call, executing the function, and then returning to the calling code. With an inline function, the compiler replaces the function call with the actual code of the function, eliminating the overhead of the function call. This results in a small performance improvement, especially for small, frequently called functions.

Explore free C++ courses

Syntax

The syntax to declare an inline function in C++ is shown below:

 
inline return_type function_name(parameter_list)
{
function_body;
}
Copy code

Where:

  • inline: This keyword is used to specify that the function is an inline function.
  • return_type: This is the data type of the value returned by the function.
  • function_name: This is the name of the function.
  • parameter_list: This is a list of parameters passed to the function. The parameters are separated by commas and are of the form data_type parameter_name.
  • function_body: This is the code that defines the behavior of the function. It is enclosed within curly braces {}.

How Does Inline in C++ Work?

In C++, inline functions work by replacing the function call with the actual code of the function. When the compiler encounters an inline function, it inserts a copy of the function code at each call site, rather than generating a separate function call.

For example, consider the following code:

 
#include
\n \n <iostream>\n \n
\n \n
\n \n
inline int max(int a, int b) {\n \n
return (a > b) ? a : b;\n \n
}\n \n
\n \n
int main() {\n \n
int x = 5, y = 10;\n \n
std::cout << "Max of " << x << " and " << y << " is: " << max(x, y) << std::endl;\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
Max of 5 and 10 is: 10
Copy code

Explanation:

In this example, the inline function max is defined to return the maximum of two integer values. The function is declared with the keyword inline to indicate that it is an inline function.

In the main function, the variables x and y are initialized to 5 and 10, respectively. The max function is then called with x and y as arguments, and the result is printed to the console.

Examples of Using Inline in C++

Example 1: Find the square of a number

 
#include
\n \n <iostream>\n \n
\n \n
\n \n
inline double square(double x) {\n \n
return x * x;\n \n
}\n \n
\n \n
int main() {\n \n
double num;\n \n
std::cout << "Enter a number: ";\n \n
std::cin >> num;\n \n
std::cout << "Square of " << num << " is: " << square(num) << std::endl;\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
Enter a number: 6
Square of 6 is: 36
Copy code

In this example, the inline function square is defined to return the square of a double value. The function is declared with the keyword inline to indicate that it is an inline function.

In the main function, the variable num is used to store the value entered by the user. The user is prompted to enter a number using the cout stream, and the value is read using the cin stream. The square function is then called with num as an argument, and the result is printed to the console.

Example 2: Find Sum of two numbers

 
#include
\n \n <iostream>\n \n
\n \n
\n \n
inline int add(int a, int b) {\n \n
return a + b;\n \n
}\n \n
\n \n
int main() {\n \n
int x, y;\n \n
std::cout << "Enter first number: ";\n \n
std::cin >> x;\n \n
std::cout << "Enter second number: ";\n \n
std::cin >> y;\n \n
std::cout << "Sum of " << x << " and " << y << " is: " << add(x, y) << std::endl;\n \n
return 0;\n \n
}\n \n
\n \n </iostream>
Copy code

Output:

 
Enter first number: 34
Enter second number: 56
Sum of 34 and 56 is 90
Copy code

In this example, the inline function add is defined to return the sum of two integer values. The function is declared with the keyword inline to indicate that it is an inline function.

In the main function, the variables x and y are used to store the values entered by the user. The user is prompted to enter two numbers using the cout stream, and the values are read using the cin stream. The add function is then called with x and y as arguments, and the result is printed to the console.

Advantages of using Inline Function in C++

Here are some of the advantages of using inline functions in C++:

  • Increased performance: Inline functions are expanded in-line with the code, reducing the overhead of a function call and increasing the program’s overall performance.
  • Improved readability: Inline functions can make code easier to read by encapsulating a small, frequently used operation into a named function, rather than having the same operation repeated in multiple places.
  • Reduced code size: Inline functions can reduce the size of the generated machine code by eliminating the need for separate function calls, saving memory and improving cache usage.
  • Better control over optimization: Inline functions give the programmer more control over optimization, allowing them to make informed decisions about which functions should be inlined and which should not.
  • Improved debugging: Debugging is often easier with inline functions, as there is no need to step into a separate function.

Disadvantages of using Inline Function in C++

Here are some of the disadvantages of using inline functions in C++:

  • Code bloat: Overuse of inline functions can result in larger executables and increased memory usage, as the code for each inline function is included in every location where it is used.
  • Reduced optimization opportunities: Inlining functions can limit the ability of the compiler to perform certain optimizations, such as inlining functions with loops or function calls.
  • Increased build times: Large programs with many inline functions can take longer to build, as the compiler must process the code for each inline function every time it is used.
  • Reduced maintainability: Inline functions can make code harder to maintain, as changes to an inline function must be made in every location where the function is used, rather than in a single location.
  • Increased complexity: Inline functions can make code more complex, as the code for each inline function must be available in every location where it is used, rather than in a single, separate function.
Understanding Substring Function in C++
Understanding Substring Function in C++
A substring in C++ is a portion of a string that can be extracted using the substr() function. The substring function in C++ allows you to extract a specific segment...read more
Power Function in C
Power Function in C
Have you ever wondered how mathematical calculations involving exponents are handled in the C programming language? One of the key functions used for this purpose is the pow() function, which...read more
Ceil function in C++
Ceil function in C++
The ceil function in C++ is a mathematical function that rounds a decimal number up to the nearest integer. It is a part of the C++ Standard Template Library (STL)...read more

Endnotes

In conclusion, inline functions are an important feature in C++ that can provide significant performance benefits by reducing the overhead of function calls and increasing the program’s overall performance. Hope this article helped you grasp the concept of inline in C++. Explore our C++ articles to find out more about the language and consolidate your knowledge of the fundamentals.  

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio