Inline Function in C++ With Examples
Did you ever think about why C++ programming language has gained such popularity? The answer to this question sums up the wide range of extensive features that it delivers. Inline function in C++ is one of the many extensive features that this programming language offers. The features offered by this language help the programmers enhance their existing programs. In this blog, we will talk about this function of C++ programming in depth and also discuss the examples, advantages, and limitations that it has to offer.
Inline Function: An Overview
The inline function is one of the many features in C++. If you are working with C++ programming and want to enhance your existing program. It will make your program run faster and speed it up by implying the in-built features.
This blog will walk you through the features and exclusive benefits that the inline function has to offer. In the upcoming sections, we will talk about why is the inline function used, its syntax, and descriptive examples. Furthermore, we will also learn about the advantages and disadvantages that it comes with. You can learn about the application in detail through an online C++ training course.
Purpose of Inline Function
The inline function in C++ is used by many programmers to resolve many issues that they face with their programs. The function can make the program run faster with a better speed, and it can enhance the program and make the codes work smoothly.
When you use this function it works effectively to increase the efficiency of the program. It also provides solutions to the existing problems in the code and solves them quickly. It has numerous features to offer.
Also Read: C++ STL Tutorial
Inline Function Syntax
Here’s how to use inline functions in C++. The inline function program in C++ has a syntax arrangement that starts with the keyword and then continues further to the data type. The inline keyword is written at the beginning of the syntax which shows that the inline function is being used in the program. Later, it is followed by the function data type to complete the whole syntax. The following is the syntax:
Syntax
inline return_type function_name (data_type arg1, data_type arg 2)
Example:
An inline function is used to calculate the largest of 2 integers and return the result.
inline int largest (int num1, int num2) {
if (num2 < num1)
return num2;
return num1;
Inline Function In C++ Examples
The inline function examples help programmers to modify various programs in the programming language. The programmer can find the square, and area, add two numbers or find the minimum between the numbers. You can have a clearer look at these examples explained below to get a better understanding. The following are some of its examples:
Example for finding the square of the number
#include <iostream>
inline int square(int num) {
return num1 * num1;
}
int main() {
int num1 = 5;
cout << "The square of “5” is “25”;
return 0;
}
In the example mentioned above, we used the square inline function which is used to find the square of numbers given, that is, num1 and num2. The result gives us the square of the desired number after using this function to deduce the square.
Example for calculating the area of a rectangle
#include <iostream>
inline int area(int length, int width) {
return length * width;
}
int main() {
int length = 10;
int width = 20;
cout << "The area of a rectangle with length" << length << "width" << width << "is" << area(length, width);
return 0;
}
In the above-mentioned example, we used the area inline function which will be used to find the area of a rectangle using the given length and width. The result will give the area of the rectangle after using this function to deduce the input.
Example for adding two numbers
#include <iostream>
inline int add(int a, int b) {
return a + b;
}
int main() {
int num1 = 5;
int num2 = 10;
cout << "The sum of" << num1<<num2<<add(num1, num2);
return 0;
}
In this example, we used the add inline function which will be used to add the two numbers given, that is, num1 and num2. The result gives us the sum of the desired number after using this function.
Example to find a minimum of the two numbers
#include <iostream>
inline int min(int a, int b) {
return (a < b)? a:b;
}
int main() {
int num1 = 10;
int num2 = 30;
cout << "The minimum of " << num1 << num2 << min(num1, num2);
return 0;
}
In the example mentioned, we used the min inline function which will be used to find a minimum of the two numbers. The result gives us the minimum of the two numbers after using this function.
Also Read: Virtual Function In C++ With Examples
Advantages and Limitations of Inline Function
The table below points out all the advantages and limitations of the inline function:
Advantages | Limitations |
---|---|
It speeds up the program. | Its excessive use makes the program heavier. |
It enhances performance. | It may also make the header file heavy. |
It optimizes the program. | It faces major storage issues. |
The execution is faster and hence that saves time. | Its large files are useless as programmers don’t use them due to the risk of heavier programs. |
It offers various features to programmers for working on programs effectively. | It reduces the efficiency of the code. |
Conclusion
The inline functions in C++ will enable you to work faster enhancing your performance It is essential to have adequate knowledge about inline functions before using them in C++ programming.