参数和形参有什么区别?
当口头谈论方法时,我不确定是否使用“参数”或“参数”或其他词。 不管怎样,其他人都知道我的意思,但什么是正确的,这些术语的历史是什么?
我是一名 C# 程序员,但我也想知道人们是否在不同的语言中使用不同的术语。
根据记录,我是自学成才,没有计算机科学背景。 (请不要告诉我阅读代码完整< /a> 因为我问这个是为了那些还没有 Steve McConnell 一本精彩的书。)
When verbally talking about methods, I'm never sure whether to use the word argument or parameter or something else. Either way the other people know what I mean, but what's correct, and what's the history of the terms?
I'm a C# programmer, but I also wonder whether people use different terms in different languages.
For the record I'm self-taught without a background in Computer Science. (Please don't tell me to read Code Complete because I'm asking this for the benefit of other people who don't already have a copy of Steve McConnell's marvelous book.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
参数是函数定义中的变量
参数是参数的值
Parameter is a variable in a function definition
Argument is a value of parameter
它在参数(计算机编程)-维基百科中得到了完美的解释
。是类型,参数是实例。
在函数定义中,
f(x) = x*x
变量x
是一个参数; 在函数调用f(2)
中,值 ``2 是函数的参数。以及 参数 - 维基百科
在 计算机编程,的两个概念参数是常用的,被称为参数和参数——或者更正式地称为形式参数和实际参数。
例如,在函数的定义中,如
y = f(x) = x + 2,
x是形式参数(定义函数的参数)。
当函数针对给定值进行计算时,如
f(3):或 y = f(3) = 3 + 2 = 5,
是实际参数 (参数)用于通过定义的函数进行评估; 它是一个给定值(实际值),用于替换定义函数的形式参数。 (在随意使用时,术语参数和参数可能会无意中互换,从而导致错误使用。)
It's explained perfectly in Parameter (computer programming) - Wikipedia
Loosely, a parameter is a type, and an argument is an instance.
In the function definition
f(x) = x*x
the variablex
is a parameter; in the function callf(2)
the value ``2 is the argument of the function.And Parameter - Wikipedia
In computer programming, two notions of parameter are commonly used, and are referred to as parameters and arguments—or more formally as a formal parameter and an actual parameter.
For example, in the definition of a function such as
y = f(x) = x + 2,
x is the formal parameter (the parameter) of the defined function.
When the function is evaluated for a given value, as in
f(3): or, y = f(3) = 3 + 2 = 5,
is the actual parameter (the argument) for evaluation by the defined function; it is a given value (actual value) that is substituted for the formal parameter of the defined function. (In casual usage the terms parameter and argument might inadvertently be interchanged, and thereby used incorrectly.)
我想了想,发现我之前的回答是错误的。 这里有一个更好的定义
{想象一盒鸡蛋:一包香肠链接:还有一个女仆}这些代表了准备过程所需的函数的元素,称为:(使用任何名称:可以说烹饪是我的函数的名称)。
女仆是一种方法。
(你必须__call_或请求这个方法来制作早餐)(制作早餐的行为是一个函数,称为烹饪)_
鸡蛋和香肠是参数:(
因为您想吃的鸡蛋和/或香肠的数量是 __variable_ 。)_
您的决定是一个参数:
它代表您正在烹饪的所选鸡蛋和/或香肠数量的 __Value_ 。_
{助记}
_“当你打电话给女仆并要求她做早餐时,她会与你__争论_你应该吃多少鸡蛋和香肠。她担心你的胆固醇”__
(那么,争论就是价值观对于您已声明并决定传递给您的 Function 的参数组合)
I thought it through and realized my previous answer was wrong. Here's a much better definition
{Imagine a carton of eggs: A pack of sausage links: And a maid } These represent elements of a Function needed for preparation called : (use any name: Lets say Cooking is the name of my function).
A Maid is a method .
( You must __call_ or ask this method to make breakfast)(The act of making breakfast is a Function called Cooking)_
Eggs and sausages are Parameters :
(because the number of eggs and the number of sausages you want to eat is __variable_ .)_
Your decision is an Argument :
It represents the __Value_ of the chosen number of eggs and/or sausages you are Cooking ._
{Mnemonic}
_" When you call the maid and ask her to make breakfast, she __argues_ with you about how many eggs and sausages you should eating. She's concerned about your cholesterol" __
( Arguments , then, are the values for the combination of Parameters you have declared and decided to pass to your Function )
永远记住:
在接收参数时传递参数。
Always Remember that:
Arguments are passed while parameters are received.
在编辑过程中,我经常对人们忘记的事情感到困惑:结构语言是基于自然语言的。
在英语中,
“参数”是一个占位符。 他们用口语设置响应格式。 根据定义,它是呼叫的一方,限制响应。
“论点”是正在考虑的立场。 你争论你的观点:你考虑一个论点。
主要区别
论证的主题角色是主体。 参数的主题角色是接收者。
交互
将参数视为男性部分,将参数视为女性部分。 参数进入参数。
用法
参数通常用在定义中。 参数通常在调用中使用。
问题
完成句子,使其不那么不和谐。
(A) 谈到定义:
(B) 说到调用:
答案
(A)
(B)
重叠
正如你可以想象的那样,回答后:在口语中,这些词有时会产生相同的反应!
因此,通常来说:
通常,如果有人想要参数信息,他们想了解有关类型、变量名称等的更多信息。如果您只提供示例参数,他们可能会感到困惑。
In editing, I'm often put off at how people forget: structure languages are based on natural languages.
In English
A "parameter" is a placeholder. They set the response format, in spoken language. By definition, it's party to the call, limiting the response.
An "argument" is a position that is being considered. You argue your opinion: you consider an argument.
Main difference
The thematic role of an argument is agent. The thematic role of parameter is recipient.
Interactions
Think of the argument as the male part, making the parameter the female part. The argument goes into the parameter.
Usage
A parameter is usually used in definitions. An argument is usually used in invocations.
Questions
Finish the sentence to make it less dissonant.
(A) Speaking of a definition:
(B) Speaking of an invocation:
Answers
(A)
(B)
Overlaps
As you can imagine, after answering: in spoken language, these words will sometimes produce identical responses!
So, as a rule:
Usually if someone wants parameter information, they want to know more about the type, the variable name, etc. They may become confused if you only give example arguments.
或者,在方法的可选参数的情况下,这样记住可能更简单:
parameter
是参数,它的值,"argument"
是参数:)Or maybe it's even simpler to remember like this, in case of optional arguments for a method:
parameter
is the parameter, its value,"argument"
is the argument :)这个例子可能会有所帮助。
This example might help.
函数/方法的参数向您描述它用于计算其结果的值。
函数的参数是在函数/方法的特定调用期间分配给这些参数的值。
The parameters of a function/method describe to you the values that it uses to calculate its result.
The arguments of a function are the values assigned to these parameters during a particular call of the function/method.
这些术语在某种程度上可以互换。 其他答案中描述的区别可以使用术语“形式参数”(表示函数体内使用的名称)和“参数”(表示调用站点提供的值)来更正确地表达( 正式论证和论证也很常见)。
另请注意,在数学中,术语参数更为常见,参数通常意味着完全不同的东西(尽管参数方程中的参数本质上是两个或多个函数的参数)。
The terms are somewhat interchangeable. The distinction described in other answers is more properly expressed with the terms formal parameter for the name used inside the body of the function and parameter for the value supplied at the call site (formal argument and argument are also common).
Also note that, in mathematics, the term argument is far more common and parameter usually means something quite different (though the parameter in a parametric equation is essentially the argument to two or more functions).
参数和参数
资料来源:Absolute C++,Walter Savitch
即
Parameters and Arguments
Source: Absolute C++, Walter Savitch
That is,
参数是参数的实例化。
An argument is an instantiation of a parameter.
是的! 参数和参数有不同的含义,可以简单地解释如下:
函数参数是函数定义中列出的名称。
函数参数是传递给函数(并由函数接收)的实际值。
Yes! Parameters and Arguments have different meanings, which can be easily explained as follows:
Function Parameters are the names listed in the function definition.
Function Arguments are the real values passed to (and received by) the function.
参数是p赋予p参数的实际值。
Arguments are actual values passed to parameters.
我对所有这些答案仍然不满意。 他们都开始谈论“函数声明”,而我的猴子大脑已经开始思考独角兽了。 这根本不能帮助我记住,这只是定义。
我想要一些可以立即并永远留在我脑海中的东西。
我很快理解的这里唯一的答案是:
参数更容易定义,只要您知道它们是什么,那么您就知道参数是另一个。
我能想到的另一种方式是:
简化为:
如果有人不同意我的观点,你可以把你的参数放在外面;)
I'm still not happy with all these answers. They all start talking about "function declarations" and my monkey brain has already wandered off and started thinking about unicorns. That doesn't help me remember at all, it's just the definition.
I want something that I can immediately and forever hold in my head.
The only answer here that I quickly understand is:
Arguments are easier to define and as long as you know what they are then you know parameters are the other.
The other way I can think of it is:
Simplified down to:
If any one wants to disagree with me, you can leave your arguments outside ;)
无需代码的简单解释
“参数”是一个非常普遍、广泛的事物,但“参数:是一个非常具体、具体的事物。这可以通过日常示例来最好地说明:
示例 1:自动售货机 - 钱是参数,$2.00 是参数
大多数机器接受输入并返回输出。例如,自动售货机将金钱作为输入,并返回碳酸饮料作为输出。在这种特殊情况下,它接受参数:金钱
。参数?如果我将 $2.00 放入机器中,则参数为: $2.00 - 这是使用的非常具体的输入。
示例 2:汽车 - 汽油是参数
让我们考虑一辆汽车:它们接受汽油(无铅汽油)作为输入。可以说,这些机器接受类型为“汽油”的参数。参数将是我输入到我的汽车中的精确且具体的输入。参数将是:40 升无铅汽油/汽油
示例 3 - 参数的详细说明
参数是输入的特定且具体的示例。 假设我的机器将一个人作为输入并将其变成一个不是骗子的人。
那么什么是论证呢? 参数将是实际放入机器中的特定人。 例如,如果将 Colin Powell 放入机器中,则参数将是 Colin Powell。
因此,参数将是作为抽象概念的人,但参数始终是具有特定名称<的特定人< /strong> 谁被放入机器中。 论证是具体的、具体的。
这就是区别。 简单的。
使困惑?
发表评论,我会修正解释。
Simple Explanations without code
A "parameter" is a very general, broad thing, but an "argument: is a very specific, concrete thing. This is best illustrated via everyday examples:
Example 1: Vending Machines - Money is the parameter, $2.00 is the argument
Most machines take an input and return an output. For example a vending machine takes as an input: money, and returns: fizzy drinks as the output. In that particular case, it accepts as a parameter: money.
What then is the argument? Well if I put $2.00 into the machine, then the argument is: $2.00 - it is the very specific input used.
Example 2: Cars - Petrol is the parameter
Let's consider a car: they accept petrol (unleaded gasoline) as an input. It can be said that these machines accept parameters of type: petrol. The argument would be the exact and concrete input I put into my car. e.g. In my case, the argument would be: 40 litres of unleaded petrol/gasoline.
Example 3 - Elaboration on Arguments
An argument is a particular and specific example of an input. Suppose my machine takes a person as an input and turns them into someone who isn't a liar.
What then is an argument? The argument will be the particular person who is actually put into the machine. e.g. if Colin Powell is put into the machine then the argument would be Colin Powell.
So the parameter would be a person as an abstract concept, but the argument would always be a particular person with a particular name who is put into the machine. The argument is specific and concrete.
That's the difference. Simple.
Confused?
Post a comment and I'll fix up the explanation.
它们在 C 中的用法没有太大区别,这两个术语都使用
在实践中。
大多数参数经常与函数一起使用。 函数调用语句传递的值称为参数,参数是在函数定义中复制该值的变量(称为形式参数)。
在上面的代码中,
num1
和num2
是形式参数,a
和b
是实际参数。They both dont have much difference in usage in C, both the terms are used
in practice.
Mostly arguments are often used with functions. The value passed with the function calling statement is called the argument, And the parameter would be the variable which copies the value in the function definition (called as formal parameter).
In the above code
num1
andnum2
are formal parameters anda
andb
are actual arguments.Oracle 的 Java 教程这样定义了这种区别:
“参数是指方法声明中的变量列表。参数是调用方法时传入的实际值。调用方法时,使用的参数必须在类型和顺序上与声明的参数匹配。”
参数和参数的更详细讨论:
https://docs.oracle.com/javase/tutorial/java/javaOO /arguments.html
Oracle's Java tutorials define this distinction thusly:
"Parameters refers to the list of variables in a method declaration. Arguments are the actual values that are passed in when the method is invoked. When you invoke a method, the arguments used must match the declaration's parameters in type and order."
A more detailed discussion of parameters and arguments:
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
或者更简单...
参数输入!
参数输出!
Or even simpler...
Arguments in !
Parameters out !
从逻辑上讲,我们实际上在谈论同一件事。
但我认为一个简单的比喻将有助于解决这个困境。
如果这些隐喻可以称为各种连接点,我们可以将它们等同于墙上的插头点。
在这种情况下,我们可以考虑如下的参数和参数;
参数是插头的插座,可以采用各种不同的形状。 但只有某些类型的插头适合它们。
参数将是插入插头点/插座以激活某些设备的实际插头。
Logically speaking,we're actually talking about the same thing.
But I think a simple metaphor would be helpful to solve this dilemma.
If the metaphors can be called various connection point we can equate them to plug points on a wall.
In this case we can consider parameters and arguments as follows;
Parameters are the sockets of the plug-point which may take various different shapes. But only certain types of plugs fit them.
Arguments will be the actual plugs that would be plugged into the plug points/sockets to activate certain equipments.
当我们在Java中创建方法(函数)时,方法是这样的。
方法的数据类型名称(data-type variable-name)
括号里的这些是参数,当我们调用方法(函数)时,我们传递这个参数的值,这些参数称为参数。
When we create the method (function) in Java, the method like this..
data-type name of the method (data-type variable-name)
In the parenthesis, these are the parameters, and when we call the method (function) we pass the value of this parameter, which are called the arguments.
根据 Joseph 的 Alabahari 著作“C# in a Nutshell”(C# 7.0,第 49 页):
在某些人类语言(据我所知,意大利语、俄语)中,这些术语广泛使用同义词。
在我的大学里,教授使用这两种名称。
According to Joseph's Alabahari book "C# in a Nutshell" (C# 7.0, p. 49) :
In some human languages (afaik Italian, Russian) synonyms are widely used for these terms.
In my university professors use both kind of names.
参数是方法定义中的变量。 调用方法时,参数是传递给方法参数的数据。
A parameter is a variable in a method definition. When a method is called, the arguments are the data you pass into the method's parameters.
参数是函数声明中的变量。
参数是传递给函数的该变量的实际值。
Parameter is the variable in the declaration of the function.
Argument is the actual value of this variable that gets passed to the function.
简单:
Simple:
参数是函数声明中的变量。
参数是传递给函数的变量的实际值。
< /a>
A parameter is a variable in the declaration of the function.
An argument is the actual value of the variable that gets passed to the function.
已经有一个关于该主题的维基百科条目(请参阅参数),它定义并区分了术语参数和参数。 简而言之,参数是函数/过程/方法签名的一部分,参数是在运行时和/或调用站点为参数提供的实际值。
维基百科文章还指出,这两个术语经常同义词使用(特别是在非正式地推理代码时):
假设以下 C 语言示例函数将两个整数相加,
x
和y
将被称为其参数:在调用站点使用
add
,如下所示的示例,123 和 456 将被称为调用的参数。此外,某些语言规范(或正式文档)选择专门使用参数或参数,并使用诸如正式和实际之类的形容词em> 来消除两种情况之间的歧义。 例如,C/C++ 文档通常将函数参数称为形式参数,将函数调用参数称为实际参数 。 有关示例,请参阅 形式和实际参数” href="http://msdn.microsoft.com/en-us/library/3bstk3k5.aspx" rel="noreferrer">Visual C++ 语言参考。
There is already a Wikipedia entry on the subject (see Parameter) that defines and distinguishes the terms parameter and argument. In short, a parameter is part of the function/procedure/method signature and an argument is the actual value supplied at run-time and/or call-site for the parameter.
The Wikipedia article also states that the two terms are often used synonymously (especially when reasoning about code informally):
Given the following example function in C that adds two integers,
x
andy
would be referred to as its parameters:At a call-site using
add
, such as the example shown below, 123 and 456 would be referred to as the arguments of the call.Also, some language specifications (or formal documentation) choose to use parameter or argument exclusively and use adjectives like formal and actual instead to disambiguate between the two cases. For example, C/C++ documentation often refers to function parameters as formal arguments and function call arguments as actual arguments. For an example, see “Formal and Actual Arguments” in the Visual C++ Language Reference.
参数是调用函数时必须填写的内容。 你放进去的就是论证。
简单设置一下:argument 进入参数,argument 就是参数的值。
更多信息:
http://en.wikipedia.org/wiki/Parameter_(computer_science)#Parameters_and_arguments
A parameter is something you have to fill in when you call a function. What you put in it is the argument.
Simply set: the argument goes into the parameter, an argument is the value of the parameter.
A bit more info on:
http://en.wikipedia.org/wiki/Parameter_(computer_science)#Parameters_and_arguments
假设您是一家航空公司。 你建造一架飞机。 你在里面安装座位。 然后,你把飞机装满乘客,然后把它送到某个地方。 乘客下船。 第二天,您再次使用同一架飞机和相同的座位,但这次搭载了不同的乘客。
飞机是你的职责。
参数是座位。
争论的焦点是坐在这些座位上的乘客。
Let's say you're an airline. You build an airplane. You install seats in it. Then, you fill the plane up with passengers and send it somewhere. The passengers disembark. Next day, you re-use the same plane, and same seats, but with different passengers this time.
The plane is your function.
The parameters are the seats.
The arguments are the passengers that go in those seats.
MCSD 认证工具包(考试 70-483)C# 编程,第一版,Wrox,2013
真实案例场景
MCSD Cerfification Toolkit (Exam 70-483) Programming in C#, 1st edition, Wrox, 2013
Real-world case scenario
一般来说,术语参数和实参可互换使用,表示传递到函数中的信息。
然而,从函数的角度来看:
Generally speaking, the terms parameter and argument are used interchangeably to mean information that is passed into a function.
Yet, from a function's perspective: