argument和parameter的区别

读一些英文技术文章的时候,很多时候,发现有argument、parameter混用的情况。以前上学的时候,也碰到过类似的情况。为了彻底弄清楚,这两者的区别,我特意在英文社区里面查了很多东西。其中微软的一个解释,我最符合我的心意。

简单点说,一个是实参,一个是形参。通常来说,实参是函数调用时候的参数,形参是函数定义和实现的使用。

一个英文定义

这个定义在很多编程语言里面是通用的

To communicate this information to the procedure, the procedure defines a parameter, and the calling code passes an argument to that parameter. You can think of the parameter as a parking space and the argument as an automobile. Just as different automobiles can park in a parking space at different times, the calling code can pass a different argument to the same parameter every time that it calls the procedure.

parameter When you define a Function or Sub procedure, you specify a parameter list in parentheses immediately following the procedure name.
argument An argument represents the value that you pass to a procedure parameter when you call the procedure. The calling code supplies the arguments when it calls the procedure.

Java例子

在下面的方法中, a 和b 都是形参,即parameter。

publiv int add(int a,int b){
    return a + b;
}

在下面的调用例子中参数:1和2,都是实参,即argument。

add(1,2);

总结

  • 1、parameter是指函数定义中参数,而argument指的是函数调用时的实际参数。
  • 2、简略描述为:parameter=形参(formal parameter), argument=实参(actual parameter)。
  • 3、在不很严格的情况下,现在二者可以混用。

发表评论

邮箱地址不会被公开。 必填项已用*标注