This document discusses inline functions in C++. Inline functions avoid code duplication and memory overhead by copying the function body in place of calls during compilation. This eliminates function call overhead but may not be suitable if the function contains static variables, loops, switches, gotos, or is recursive. The example demonstrates declaring inline float and double math functions and calling them to output results without function call overhead. Benefits of inline functions are that they are better than macros, eliminate function call overhead, improve readability, and allow more efficient program execution.
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More
This document discusses inline functions in C++. Inline functions avoid code duplication and memory overhead by copying the function body in place of calls during compilation. This eliminates function call overhead but may not be suitable if the function contains static variables, loops, switches, gotos, or is recursive. The example demonstrates declaring inline float and double math functions and calling them to output results without function call overhead. Benefits of inline functions are that they are better than macros, eliminate function call overhead, improve readability, and allow more efficient program execution.
This document discusses inline functions in C++. Inline functions avoid code duplication and memory overhead by copying the function body in place of calls during compilation. This eliminates function call overhead but may not be suitable if the function contains static variables, loops, switches, gotos, or is recursive. The example demonstrates declaring inline float and double math functions and calling them to output results without function call overhead. Benefits of inline functions are that they are better than macros, eliminate function call overhead, improve readability, and allow more efficient program execution.
This document discusses inline functions in C++. Inline functions avoid code duplication and memory overhead by copying the function body in place of calls during compilation. This eliminates function call overhead but may not be suitable if the function contains static variables, loops, switches, gotos, or is recursive. The example demonstrates declaring inline float and double math functions and calling them to output results without function call overhead. Benefits of inline functions are that they are better than macros, eliminate function call overhead, improve readability, and allow more efficient program execution.
Steps needed to perform when a function is called,
◦ Jumping to the function ◦ Saving into registers ◦ Pushing arguments into stacks ◦ Return back to calling function Needs extra time to perform the specified task. Macros are the only solution to save memory space But macros are not a function that wont carried out the task of error checking during compilation Inline function Primary Factors:
◦ Code Duplication in program is avoided.
◦ Memory space is saved. ◦ C++ provides a mechanism called inline function . When a function is declared as inline the compiler copies the code of the function in calling function i.e function body is inserted in place of function call during compilation. ◦ Passing of control between coller and collee function is avoided. The compiler replaces the function call with actual code. Syntax : Inline function-header { function body; } Eg : inline double cube(double a) { return( a * a * a); } C = cube(2.5); D = cube(2.5 + 1.5);
Inline function may not suitable for the following situations
For functions returning values, if a loop, switch, or goto exists. For functions not returning values, if return statement exists. If functions contain static variables. If inline functions are recursive. #include<iostream.h> Using namespace std; Inline float mul(float x, float y) { Return (x * y); } Inline double div(double p, double q) { Return(p/q); } int main() { Float a = 12.345; Float b = 9.82; Cout<<mul(a,b); Cout<<div(a,b); Return 0; } The benefits of inline functions are as follows : 1. Better than a macro. 2. Function call overheads are eliminated. 3. Program becomes more readable. 4. Program executes more efficiently.
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More