
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Benefits of inline functions in C++?
C++ inline function is a powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.
Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality.
To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.
A function definition in a class definition is an inline function definition, even without the use of the inline specifier.
Following is an example, which makes use of inline function to return max of two numbers −
Example
#include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main() { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; }
Output
Max (20,10): 20 Max (0,200): 200 Max (100,1010): 1010
Now let us see, what are the benefits of using the inline functions in our code −
Here the function call overhead does not occur.
It saves the overhead of the push and pop variables on the stack, when the function is called.
It saves the overhead of return call from a function.
When one inline function is created, the compiler may perform context specific optimization on the body of the function. This kind of optimizations are not performed for normal functions.
Using of small inline functions may be useful for embedded systems, because inline can yield less code than the function call preamble and return.
- Related Articles
- Inline Functions in C++
- Are there inline functions in Java?
- Activated Charcoal: Functions, Benefits, Risks
- What is the difference between anonymous and inline functions in JavaScript?
- Inline-level Elements and Inline Boxes in CSS
- What Are Carbohydrates? Benefits Functions Best Sources & Low-Carb Diets
- Inline Subheadings in Bootstrap
- How to remove the space between inline/inline-block elements in HTML?
- Inline virtual function in C++
- list-inline class in Bootstrap
- Bootstrap Inline Form
- Working with Inline CSS
- Checkbox-inline Bootstrap class
- Bootstrap .checkbox-inline class
- Bootstrap .radio-inline class
