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

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

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

总结

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


当前回答

考虑下面的java代码。

public class Test{
   public String hello(String name){
      return "Hello Mr."+name;
   }

  public static void main(String args[]){
     Test test = new Test();
     String myName = "James Bond";
     test.hello(myName);
  }
}

hello(String name)的方法定义声明了一个名为name的String参数。 在main方法中,我们通过传入参数myName来调用hello方法。

parameter是占位符,而argument是方法的实际值。

其他回答

参数和参数

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

也就是说,

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

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);

参数是在调用函数时必须填写的东西。你放进去的是论证。

简单设置:实参进入形参,实参就是形参的值。

更多信息: http://en.wikipedia.org/wiki/Parameter_ (computer_science) # Parameters_and_arguments

或者,在方法有可选参数的情况下,这样记会更简单:

public void Method(string parameter = "argument") 
{

}

Parameter是参数,它的值,"argument"是实参:)

我对这些答案还是不满意。他们都开始谈论“函数声明”,我的猴子大脑已经走神了,开始思考独角兽。这根本不能帮助我记住,这只是定义。

我想要一些我可以立即并且永远记在脑子里的东西。

我很快就能理解的唯一答案是:

参数是传入的实际值。

参数更容易定义,只要你知道它们是什么,你就知道参数是另一个。

另一种思考方式是

参数是函数之外的变量 参数是函数内部的变量

简化为:

参数在外面,参数在里面

如果有人不同意我的观点,你可以把你的论点放在外面,把你的参数带进来;)