当口头谈论方法时,我从不确定是否应该使用参数或参数或其他词。不管怎样,其他人都知道我的意思,但什么是正确的,这些术语的历史是什么?
我是一名c#程序员,但我也想知道人们是否在不同的语言中使用不同的术语。
声明一下,我是自学的,没有计算机科学的背景。(请不要让我去读《代码完成》,因为我这么做是为了让那些还没有史蒂夫·麦康奈尔这本了不起的书的人受益。)
总结
普遍的共识似乎是,在团队环境中可以互换使用这些术语。除非你在定义精确的术语;然后你也可以使用“正式参数/参数”和“实际参数/参数”来消除歧义。
函数的形式形参在函数声明中列出,并在函数定义的主体中使用。形式形参(任何类型的)是一种空白或占位符,在调用函数时用一些东西填充。
实参是用来填充形式形参的。当你写下一个函数调用时,实参会在函数名后面的括号中列出。在执行函数调用时,将插入形式形参的实参。
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.
参数和参数
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
也就是说,
你得回到最基本的问题上来。构造函数和方法都有形参和实参。很多人甚至把构造函数称为特殊的方法。这是一个方法声明参数的使用方式:
type name(parameters){
//body of method
}
这是构造函数声明参数的使用方式:
classname(parameters){
//body
}
现在让我们看一个计算立方体体积的示例代码:
public class cuboid {
double width;
double height;
double depth;
cuboid(double w,double h,double d) {
//Here w,h and d are parameters of constructor
this.width=w;
this.height=h;
this.depth=d;
}
public double volume() {
double v;
v=width*height*depth;
return v;
}
public static void main(String args[]){
cuboid c1=new cuboid(10,20,30);
//Here 10,20 and 30 are arguments of a constructor
double vol;
vol=c1.volume();
System.out.println("Volume is:"+vol);
}
}
现在你明白了,当我们在代码后面的某个地方调用对象的构造函数/方法时,传递的是参数而不是形参。因此,形参仅限于定义逻辑对象的地方,而实参则在实际创建物理对象时发挥作用。