Home  >  Article  >  Backend Development  >  Common programming techniques for passing function parameters in C++

Common programming techniques for passing function parameters in C++

WBOY
WBOYOriginal
2024-04-12 22:18:01404browse

The passing methods of function parameters in C include: passing by value (copying the value of the actual parameter) passing by reference (directly operating the value of the actual parameter) passing the pointer (directly accessing the address of the actual parameter) passing by const reference (reading the value of the actual parameter) value, but cannot be modified)

C++ 函数参数传递方式的常见编程技巧

C Common programming tips for passing function parameters

In C, function parameters can be passed There are four ways to pass: pass by value, pass by reference, pass by pointer and pass by const reference. For different needs, different delivery methods are more suitable.

Call by Value

Passing value is the simplest and most direct way, that is, copying the value of the actual parameter inside the function. The advantage is that changes to the actual parameters are avoided from affecting the value of the actual parameters themselves. Example:

void increment(int num) {
  num++;  // 只能更改 num 的局部副本
}

int main() {
  int x = 5;
  increment(x);  // x 的副本被传递给 increment()
  cout << x;  // 输出 5,x 不会改变
}

Call by Reference

Passing a reference allows the value of the actual parameter to be directly manipulated within the function. The advantage is that it avoids the value transfer process and improves efficiency. Example:

void increment(int& num) {
  num++;  // 直接更改实参的值
}

int main() {
  int x = 5;
  increment(x);  // x 的引用被传递给 increment()
  cout << x;  // 输出 6
}

Call by Pointer

Passing a pointer allows direct access to the address of the actual parameter within the function. The advantage is that it can perform more flexible operations on the content pointed to by the pointer. Example:

void swap(int* a, int* b) {
  int temp = *a;
  *a = *b;
  *b = temp;
}

int main() {
  int x = 5, y = 7;
  swap(&x, &y);  // 传递 x 和 y 的地址
  cout << x << " " << y;  // 输出 7 5
}

Call by const Reference

Passing a const reference is similar to passing a reference, but the function cannot modify the value of the actual parameter. The advantage is that accidental modifications are avoided and code security is improved. Example:

void print(const int& num) {
  cout << num;  // 只读访问实参
}

int main() {
  int x = 5;
  print(x);  // x 的 const 引用被传递给 print()
}

Practical case

When calculating the area of ​​a circle, the parameter of the function can be the radius of the circle. In order to avoid the extra overhead caused by passing a value, you can choose to pass a reference and let the function directly operate on the radius value.

#include <cmath>

double calculateArea(double& radius) {
  return M_PI * radius * radius;
}

int main() {
  double radius;
  cout << "Enter the radius of the circle: ";
  cin >> radius;
  cout << "The area of the circle is: " << calculateArea(radius) << endl;
}

The above is the detailed content of Common programming techniques for passing function parameters in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn