1. Function call principle
"The final product of the compilation process is an executable program--consisting of a set of machine language directives. When you run the program, the operating system loads the instructions into the computer's memory, so each instruction has a specific memory address. The computer then steps through the instructions. Sometimes (when there are loops and branching statements), some instructions are skipped, jumping forward or backward to a specific address. Regular function calls also cause the program to jump to another address (the address of the function) and return at the end of the function. A typical implementation of this process is described in more detail below. when executing to a function call instruction, the program stores the memory address of the instruction immediately after the function call and copies the function arguments to the stack (for this reserved block of memory), jumps to the memory unit that marks the beginning of the function, executes the function code (perhaps the return value is placed in the register), and jumps back to the command where the address is saved (this is the same as reading the article and pausing to look at the footnote and returning to the place where you read it before reading the footnote). Jumping back and forth and recording the jump position means that there is a cost to using the function previously. "
2. Inline functions
An inline function provides another option. The compiler will replace the function call with the corresponding function code . As a result, inline functions run slightly faster than regular functions, but at the cost of requiring more memory.
3. Use of inline functions
Insert the keyword inline before the function declaration;
Add the keyword inline before the function definition.
Examples are as follows:
#include <iostream>inlineDoubleSquareDoublex) {returnx*x;}intMain () {using namespacestd; Doubleb; Doublec =13.0; A= Square (5.0); b= Square (4.5+7.5); cout<<"a="<<a<<", b="<<b<<Endl; cout<<"c="<<c<<Endl; cout<<"C squared="<<square (c + +) <<Endl; cout<<"Now c="<<c<<Endl; return 0;}
The program output results are as follows:
a=25,b=144
C=13
C square=169
Now c=14
4. The difference between inline functions and macro definitions
The C language uses the preprocessor statement # define to provide macros. As shown in the following example:
#define SQUARE (X) x*x
When the macro definition is replaced by text, the implemented--x is the symbol marker of the parameter.
A = Square (5.0);->a=5.0*5.0;
b = Square (4.5+7.5);->b=4.5+7.5*4.5+7.5
d = Square (c + +);->d=c++*c++
As you can see, for B, you need to use parentheses to operate normally.
#define SQUARE (x) ((x) * (x))
For C, it's still incrementing two times.
Therefore, there is an essential difference between the macro definition and the inline function, and the conversion should be considered whether the function is normal after conversion .
5. When to use inline functions
If the function code is executed longer than the function call mechanism, the time savings are very small. If the code execution time is short, the inline function can save time on the function call.
Reference: "C + + primer.plus" pp.253-255
A detailed description of C + + inline functions