当口头谈论方法时,我从不确定是否应该使用参数或参数或其他词。不管怎样,其他人都知道我的意思,但什么是正确的,这些术语的历史是什么?

我是一名c#程序员,但我也想知道人们是否在不同的语言中使用不同的术语。

声明一下,我是自学的,没有计算机科学的背景。(请不要让我去读《代码完成》,因为我这么做是为了让那些还没有史蒂夫·麦康奈尔这本了不起的书的人受益。)

总结

普遍的共识似乎是,在团队环境中可以互换使用这些术语。除非你在定义精确的术语;然后你也可以使用“正式参数/参数”和“实际参数/参数”来消除歧义。


当前回答

我仔细想了想,意识到我之前的答案是错的。这里有一个更好的定义

{想象一盒鸡蛋:一包香肠链接:和一个女佣}这些代表一个函数的元素,需要准备称为:(使用任何名称:假设烹饪是我的函数的名称)。

Maid是一种方法。

(你必须调用或询问这个方法来做早餐)(做早餐的行为是一个叫做烹饪的功能)_

鸡蛋和香肠

(因为你想吃的鸡蛋和香肠的数量是可变的。

你的决定是一个论点:

它表示你正在烹饪的鸡蛋和/或香肠的数量。_

{助记}

“当你打电话给女仆让她做早餐时,她和你争论你应该吃多少鸡蛋和香肠。她担心你的胆固醇__

(然后,参数是你声明并决定传递给你的函数的参数组合的值)

其他回答

假设你是一家航空公司。你造了一架飞机。你在里面安装座椅。然后,你把飞机装满乘客,然后把它送到某个地方。乘客们下了船。第二天,你再次乘坐同一架飞机,同样的座位,但这次乘客不同。

平面是你的函数。

参数是座位。

争论的焦点是坐在座位上的乘客。

function fly(seat1, seat2) {
    seat1.sayMyName();
    // Estraven
    seat2.sayMyName();

    etc.
}

var passenger1 = "Estraven";
var passenger2 = "Genly Ai";

fly(passenger1, passenger2); 

简单:

PARAMETER→占位符(这意味着一个占位符属于函数命名,用于函数体中) 参数→实际值(这意味着调用函数传递的实际值)

这些术语在某种程度上是可以互换的。其他答案中描述的区别用术语形式形参表示函数体中使用的名称,用术语形参表示调用站点提供的值(形式实参和实参也很常见)更为恰当。

还要注意的是,在数学中,参数(argument)这个词更常见,而参数(parameter)通常意味着完全不同的东西(尽管参数方程中的参数本质上是两个或多个函数的参数)。

参数和参数

All the different terms that have to do with parameters and arguments can be confusing. However, if you keep a few simple points in mind, you will be able to easily handle these terms. The formal parameters for a function are listed in the function declaration and are used in the body of the function definition. A formal parameter (of any sort) is a kind of blank or placeholder that is filled in with something when the function is called. An argument is something that is used to fill in a formal parameter. When you write down a function call, the arguments are listed in parentheses after the function name. When the function call is executed, the arguments are plugged in for the formal parameters. The terms call-by-value and call-by-reference refer to the mechanism that is used in the plugging-in process. In the call-by-value method only the value of the argument is used. In this call-by-value mechanism, the formal parameter is a local variable that is initialized to the value of the corresponding argument. In the call-by-reference mechanism the argument is a variable and the entire variable is used. In the call- by-reference mechanism the argument variable is substituted for the formal parameter so that any change that is made to the formal parameter is actually made to the argument variable.

来源:绝对的c++, Walter Savitch

也就是说,

维基百科上已经有关于这个主题的条目(参见Parameter),其中定义和区分了术语Parameter和argument。简而言之,形参是函数/过程/方法签名的一部分,而实参是在运行时和/或调用时为形参提供的实际值。

维基百科的文章还指出,这两个术语通常是同义词(特别是在非正式地对代码进行推理时):

虽然参数也很常见 称为论点,论点 更恰当的说法是 指定的实际值或引用 参数变量时 子例程在运行时被调用。

给定以下C语言中两个整数相加的示例函数,x和y将被称为它的参数:

int add(int x, int y) {
    return x + y;
}

在使用add的调用站点中,例如下面所示的示例,123和456将被引用为调用的参数。

int result = add(123, 456);

此外,一些语言规范(或正式文档)选择专门使用参数或参数,并使用像正式和实际这样的形容词来消除两种情况之间的歧义。例如,C/ c++文档经常将函数形参作为正式实参,将函数调用实参作为实际实参。有关示例,请参阅Visual c++语言参考手册中的“正式和实际参数”。