gpt4 book ai didi

c++ - 在不同的函数中使用相同的参数

转载 作者:行者123 更新时间:2023-11-28 01:48:19 25 4
gpt4 key购买 nike

所以我对一些事情很好奇。我有两个函数,都按照我希望的方式工作,它们如下:

//this function returns the absolute value of a number
int abs(int y) {
if (y < 0) {
return -1 * y;
}
return y;
}

//gives a random number, multiplied by x, in the range 1-y
int randomNum(int x, int y) {
srand((int)time(0));
return abs(x * rand() % y) + 1;
}

它们都可以工作,所以它们的功能不是问题。但是如果您注意到的话,它们都使用一个名为“int y”的参数。

我的问题是,尽管这两个功能有效,但这种不好的做法是否会在更复杂的程序中让我陷入困境?或者这无关紧要,因为变量是它们各自函数的局部变量?

我的意思是,如果我将其中一个“int y”参数更改为其他参数显然没什么大不了的,我只是好奇,就这样。

最佳答案

我觉得对于简单的程序来说还可以。

However, you should name a variable using the same care with which you name a first-born child.

Robert C. Martin,Clean Code:敏捷软件工艺手册

例如,没有人喜欢阅读声明 int foo(int x, int y, int z, int xx, int bb, int cc .....)

关于c++ - 在不同的函数中使用相同的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43965913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com