Change:
<!-- NOTE: If you're reporting a security issue, don't create a GitHub issue. Instead, email We will look into it immediately. -- Describe your problem and how to reproduce it: I noticed that the /guide/english/cplusplus has 2 seperate sections on 'inline functions' with much in common inbetween them. Add a Link to the page with the problem: Tell us about your browser and operating system: Browser Name: Google Chrome Browser Version: Version 69.0.3497.100 (Official Build) (64-bit) Operating System: Ubuntu 18.04.1 LTS If possible, add a screenshot here (you can drag and drop, png, jpg, gif, etc. in this box): !

Discussion:
<del> --- title: Inline Function --- # Inline Function ## Introduction Inline function is a special function defined in C++ and is expanded inline when it is called. Now, what does that exactly mean? Whenever a function is called it takes a lot of extra time for performing series of activities such as jumping to the function, saving registers, pushing arguments into stack and returning to the calling function. So it takes a lot of time. But an inline function is a function in which the compiler has been requested to perform inline expansion. Where the function requests the compiler to insert the complete body of the function in every place that the function is called, rather than generating code to call the function in the one place it is defined. However, we cannot guarantee that every function declared inline will be inline. Because when we declare a function as `inline`, it is a request, not a command. Compiler may ignore the request of inlining in the following situations:- 1) If the function contains loop e.g `for` loop, `while` loop, `do-while` loop etc. 2) If the function contains a `switch` or `goto` statement. 3) If the function doesn't return anything even if return type (other than `void` of course) is mentioned. 4) If the function contains a static variable. 5) If the function contains a recursive call. ``` c++ syntax :- --------- inline return_type function_name(argument_list){ // function body } ``` ## When to use Inline function * When the function performs small tasks and is called very often. * When performance is important. * Instead of a macro. ``` c++ #include<iostream> using namespace std; class MathOperation{ public: inline int add(int x, int y){ return(x+y); } inline float div(int n1, float n2){ return(n1/n2); } }; int main(){ MathOperation obj; cout << "Addition is :" << obj.add(34,12) << <"n"; cout << "Division is :" << obj.div(12,3.4) << "n"; return 0; } ``` ## Advantages of Inline function * It saves the overhead of return call from a function. * It increases locality of reference by utilizing instruction cache. * It speeds up your program by avoiding function call overheads. * It saves overhead of variables push/pop operations on the stack, when function calls happen. * It is possible to put a function definition in a header file, i.e. it can be included in multiple compilation unit, without the linker complaining. ## Disadvantages of inline function * When used in a header, it makes your header file larger with information which users don’t care. * It increases the executable size due to code expansion. * C++ inlining is resolved at compile time. Which means if you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated. * As mentioned above it increases the executable size, which may cause thrashing in memory. More number of page fault, bringing down your program performance. </del>

## Inline Functions in C++ <del> When the program executes the function call instruction the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register and returns control to the calling function. This can become overhead if the execution time of function is less than the switching time from the caller function to called function (callee). For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually execute the function’s code. This overhead occurs for small functions because execution time of small function is less than the switching time. </del> <ins> When the program executes the function call instruction, the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack, and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register and returns control to the calling function. This can become overhead if the execution time of function is less than the switching time from the caller function to called function (callee). For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often more than the time needed to actually execute the function’s code. This overhead occurs for small functions because execution time of small function is less than the switching time. C++ provides an inline functions to reduce the function call overhead for small functions. An inline function substitutes the code of the function where the call is being made (inline) instead of passing to the function. This substitution is performed by the C++ compiler at compile time. </ins> <del> C++ provides an inline functions to reduce the function call overhead. Inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time. Inline function may increase efficiency if it is small. </del> The syntax for defining the function inline is: ```cpp inline return-type function-name(parameters) { // function code } ``` <del> Remember, inlining is only a request to the compiler, not a command. Compiler can ignore the request for inlining. Compiler may not perform inlining in such circumstances like: * If a function contains a loop. (for, while, do-while) </del> <ins> Note that inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining. The compiler may not perform inlining in the following circumstances: * If a function contains a loop (for, while, do-while). </ins> * If a function contains static variables. * If a function is recursive. * If a function return type is other than void, and the return statement doesn’t exist in function body. * If a function contains switch or goto statement. <del> ### Inline functions provide following advantages: </del> <ins> ### When to use inline functions: * When the function performs small tasks and is called very often. * When performance is important. * Instead of a macro. </ins> <del> * Function call overhead doesn’t occur. * It also saves the overhead of push/pop variables on the stack when function is called. * It also saves overhead of a return call from a function. * When you inline a function, you may enable compiler to perform context specific optimization on the body of function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by considering the flows of calling context and the called context. * Inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function call preamble and return. </del> <ins> ``` c++ #include<iostream> </ins> <del> ### Inline function disadvantages: </del> <ins> using namespace std; </ins> <del> * The added variables from the inlined function consumes additional registers, After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization. This means that when inline function body is substituted at the point of function call, total number of variables used by the function also gets inserted. So the number of register going to be used for the variables will also get increased. So if after function inlining variable numbers increase drastically then it would surely cause an overhead on register utilization. </del> <ins> class MathOperation{ </ins> <del> * If you use too many inline functions then the size of the binary executable file will be large, because of the duplication of same code. </del> <ins> public: inline int add(int x, int y){ return(x+y); } inline float div(int n1, float n2){ return(n1/n2); } }; </ins> <del> * Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory. </del> <ins> int main(){ </ins> <del> * Inline function may increase compile time overhead if someone changes the code inside the inline function then all the calling location has to be recompiled because compiler would require to replace all the code once again to reflect the changes, otherwise it will continue with old functionality. </del> <ins> MathOperation obj; cout << "Addition is :" << obj.add(34,12) << <"n"; cout << "Division is :" << obj.div(12,3.4) << "n"; return 0; } ``` ### Advantages: </ins> <del> * Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. </del> <ins> * It saves the overhead of return call from a function. * It increases locality of reference by utilizing instruction cache. * It speeds up your program by avoiding function call overheads. * It saves overhead of variables push/pop operations on the stack, when function calls happen. * It is possible to put a function definition in a header file, i.e. it can be included in multiple compilation unit, without the linker complaining. * When you inline a function, you may enable compiler to perform context specific optimization on the body of function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by considering the flows of calling context and the called context. * Inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function call preamble and return. ### Disadvantages: * When used in a header, it makes your header file larger with information which users don’t care. * It increases the executable size due to code expansion. * C++ inlining is resolved at compile time. If you change the code of the inlined function, you would need to recompile all the code using it to make sure it will be updated. * Inline functions may not be useful for embedded systems when optimizing for memory (versus speed). </ins> * Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade. <ins> * The added variables from expanding the inlined function consumes additional registers potentially causing an overhead on register utilization if the number of variables increases drastically. * Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory. </ins> The following program demonstrates this concept: ```cpp