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

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

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

总结

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


当前回答

在参数(计算机编程)-维基百科中有完美的解释

宽泛地说,形参是一种类型,实参是一个实例。

在函数定义f(x) = x*x中,变量x是一个参数;在函数调用f(2)中,值' ' 2是函数的实参。

和参数-维基百科

在计算机编程中,通常使用两种参数的概念,它们被称为参数和实参,或者更正式地称为形式参数和实际参数。

例如,在函数的定义中如

y = f(x) = x + 2,

X是定义函数的形式参数(参数)。

当函数对给定值求值时,例如

F(3):或者,y = F (3) = 3 + 2 = 5,

被定义的函数计算的实际形参(实参);它是一个给定的值(实际值),取代了定义函数的形式参数。(在日常使用中,术语parameter和argument可能会无意中互换,从而被错误地使用。)

其他回答

从逻辑上讲,我们谈论的其实是同一件事。 但我认为一个简单的比喻将有助于解决这个困境。

如果这些比喻可以称为各种连接点,我们可以把它们等同于墙上的插头点。 在这种情况下,我们可以这样考虑参数和参数;

参数是插接点的插座,可以有各种不同的形状。但只有特定类型的插头适合它们。 参数将是插入插头点/插座以激活某些设备的实际插头。

术语参数和参数的使用被滥用了 在程序员甚至作者之间。当处理 方法中,术语参数用于标识中的占位符 方法签名,而术语参数是实际的 您传递给方法的值。

MCSD认证工具包(考试70-483)c#编程,第1版,Wrox, 2013

真实的案例

// Define a method with two parameters
int Sum(int num1, int num2)
{
   return num1 + num2;
}

// Call the method using two arguments
var ret = Sum(2, 3);

参数和参数

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是函数声明中的变量。

参数是传递给函数的这个变量的实际值。

由于我的背景和主要环境是C语言,我将从实际的C标准和一本重要的参考书中提供一些关于这个主题的陈述/引用,这本书也是C语言的一个开发人员写的,它经常被引用,通常被视为C语言的第一个非官方标准:


C编程语言(第二版),作者Brian W. Kernighan和Dennis M. Ritchie(1988年4月):

第25页,第1.7节-功能 在函数定义中,我们通常将在括号内列表中命名的变量使用形参,将在函数调用中使用的值使用形参。术语形式论证和实际论证有时用于同样的区别。

ISO/IEC 9899:2018 (C18):

3.3 论点 实际参数 DEPRECATED:实际参数 函数调用表达式中以圆括号为界的逗号分隔列表中的表达式,或者类函数宏调用中以圆括号为界的逗号分隔列表中的预处理令牌序列。


3.16 参数 形式参数 弃用:正式的参数 对象,该对象声明为函数声明或定义的一部分,在函数的入口处获取值,或者在类函数宏定义中,由紧接在宏名称后面的圆括号括起来的逗号分隔的列表中获取标识符。