英文:
Is it valid for function parameter's names to be the same as the variables used in the call to the function?
问题
在程序中,可以使用与函数参数名称相同的变量名来调用函数,这是程序上没有问题的。例如:
int num1 = 10;
int num2 = 10;
int result = add(num1, num2);
但是,为了提高代码的可读性和可维护性,通常建议使用不同的变量名来调用函数,以避免混淆。这样可以更清晰地表达变量的用途和含义。例如:
int input1 = 10;
int input2 = 10;
int sum = add(input1, input2);
英文:
Say I have a function:
int add(int num1, int num2) {
num1 += num2;
return num1;
}
is it programatically correct to call the function with variable(s) which contain the same name(s) as the function's parameter's names?
example:
int num1 = 10;
int num2 = 10;
int result = add(num1, num2)
Or is it programatically correct to use different names for the function call's variables/function parameters.
答案1
得分: 6
是的。这些变量位于不同的作用域中,因此是完全有效的。请参阅名称查找和作用域:
英文:
Yes. Those variables are in different scopes, therefore it is perfectly valid. See Name lookup and Scope:
> Each name that appears in a C++ program is only valid in some possibly
> discontiguous portion of the source code called its scope.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论