争论
一种说法是传递给函数的值当函数被调用。每当在程序执行期间调用任何函数时,都会有一些值与函数传递。这些值称为参数。与函数一起传递的参数将替换为在函数定义期间使用的那些变量,然后使用这些值执行函数。让我们看一些例子以便于理解:
例子:
public class Example {
public static int multiply(int a, int b)
{
return a + b;
}
public static void main(String[] args)
{
int x = 2;
int y = 5;
// the variables x and y are arguments
int sum = multiply(x, y);
System.out.println("SUM IS: " + sum);
}
}
输出:
SUM IS: 7
在上面的例子中的函数变量 x 和 y 是参数
参数
参数是用于在函数定义期间定义特定值的变量。每当我们定义一个函数,我们都会向编译器引入一些在该函数运行中使用的变量。这些变量通常称为Parameters 。参数和参数大多具有相同的值,但理论上彼此不同。
例子:
public class Example {
// the variables a and b are parameters
public static int multiply(int a, int b)
{
return a + b;
}
public static void main(String[] args)
{
int x = 2;
int y = 5;
int sum = multiply(x, y);
System.out.println("SUM IS: " + sum);
}
}
输出:
SUM IS: 7
在上面的例子中的函数变量 a 和 b 是参数
自变量和参数之间的区别
Argument | Parameter |
---|---|
When a function is called, the values that are passed in the call are called arguments. | The values which are written at the time of the function prototype and the definition of the function. |
These are used in function call statement to send value from the calling function to the called function. | These are used in function header of the called function to receive the value from the arguments. |
During the time of call each argument is always assigned to the parameter in the function definition. | Parameters are local variables which are assigned value of the arguments when the function is called |
They are also called Actual Parameters | They are also called Formal Parameters |