在赋值之前,我应该在 C++ 程序的顶部声明变量吗:
int i;
std::string x;
std::string retval;
x = "foo";
i = 5;
retval = somefunction();
Run Code Online (Sandbox Code Playgroud)
或者,通过以下方式为变量赋值是否正确/可接受:
int i = 5;
std::string x = "foo";
std::string retval = somefunction();
Run Code Online (Sandbox Code Playgroud)
我是 C++ 的新手,我想知道 C++ 社区接受哪种方式。
说我有一个功能:
int add(int num1, int num2) {
num1 += num2;
return num1;
}
Run Code Online (Sandbox Code Playgroud)
使用包含与函数参数名称相同的名称的变量调用函数在编程上是否正确?
例子:
int num1 = 10;
int num2 = 10;
int result = add(num1, num2)
Run Code Online (Sandbox Code Playgroud)
或者为函数调用的变量/函数参数使用不同的名称在编程上是否正确。