
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 the 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 the inline function to return a max of two numbers −
Example Code
#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
- Related Questions & Answers
- Benefits of inline functions in C++?
- Are there inline functions in Java?
- What is the difference between anonymous and inline functions in JavaScript?
- Inline virtual function in C++
- Inline-level Elements and Inline Boxes in CSS
- Difference Between Inline and Macro in C++
- How do inline variables work in C++/C++17?
- What is an inline function in C language?
- Inline Subheadings in Bootstrap
- Thread functions in C/C++
- Functions in C/C++(3.5)
- Bootstrap Inline Form
- Iterator Functions in C#
- Trigonometric Functions in C#
- Time Functions in C#