Home > Article > Backend Development > What are the C++ function calling conventions?
There are four function calling conventions in C: pass by value, pass by pointer, pass by reference and pass by RVO. Passing by value passes a copy of the parameter, passing by pointer passes the address of the parameter, passing by reference passes the reference of the parameter, and passing by RVO moves the contents of the object directly under certain conditions.
C Function calling convention
The function calling convention specifies how parameters are passed during a function call, and when the call returns How to return results. There are four main function calling conventions in C:
1. Pass-by-value
2. Pass-by-pointer
3. Pass-by-reference
4. Passing through RVO (return value optimization, return value optimization)
Practical case
// 通过值传递整数 void func_by_val(int val) { val++; // 不会影响原始值 } // 通过指针传递整数 void func_by_ptr(int *val) { (*val)++; // 会影响原始值 } // 通过引用传递整数 void func_by_ref(int &val) { val++; // 会影响原始值 } int main() { int a = 5; func_by_val(a); std::cout << a << std::endl; // 输出 5 func_by_ptr(&a); std::cout << a << std::endl; // 输出 6 func_by_ref(a); std::cout << a << std::endl; // 输出 7 }
The above is the detailed content of What are the C++ function calling conventions?. For more information, please follow other related articles on the PHP Chinese website!