当口头谈论方法时,我从不确定是否应该使用参数或参数或其他词。不管怎样,其他人都知道我的意思,但什么是正确的,这些术语的历史是什么?
我是一名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.
术语参数和参数的使用被滥用了
在程序员甚至作者之间。当处理
方法中,术语参数用于标识中的占位符
方法签名,而术语参数是实际的
您传递给方法的值。
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);
它们在C语言中的用法没有太大的区别,这两个术语都被使用了
在实践中。
大多数参数通常与函数一起使用。随函数调用语句传递的值称为实参,形参是复制函数定义中的值的变量(称为形式形参)。
int main ()
{
/* local variable definition */
int a = 100;
int b = 200;
int ret;
/* calling a function to get max value */
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
/* function returning the max between two numbers */
int max(int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}
在上面的代码中,num1和num2是形式参数,a和b是实际参数。
我仔细想了想,意识到我之前的答案是错的。这里有一个更好的定义
{想象一盒鸡蛋:一包香肠链接:和一个女佣}这些代表一个函数的元素,需要准备称为:(使用任何名称:假设烹饪是我的函数的名称)。
Maid是一种方法。
(你必须调用或询问这个方法来做早餐)(做早餐的行为是一个叫做烹饪的功能)_
鸡蛋和香肠
(因为你想吃的鸡蛋和香肠的数量是可变的。
你的决定是一个论点:
它表示你正在烹饪的鸡蛋和/或香肠的数量。_
{助记}
“当你打电话给女仆让她做早餐时,她和你争论你应该吃多少鸡蛋和香肠。她担心你的胆固醇__
(然后,参数是你声明并决定传递给你的函数的参数组合的值)