争论
参数是指调用函数时在函数内传递的值。这些值通常是在执行过程中需要参数的函数的来源。这些值被分配给被调用函数定义中的变量。函数传递的值的类型与函数定义中定义的变量的类型相同。这些也称为实际参数或实际参数。
示例:假设需要调用 sum()函数,将两个数字相加。这两个数字称为参数,并在 sum() 从其他地方调用时传递给它。
C
// C code to illustrate Arguments
#include
// sum: Function definition
int sum(int a, int b)
{
// returning the addition
return a + b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// sum() is called with
// num1 & num2 as ARGUMENTS.
res = sum(num1, num2);
// Displaying the result
printf("The summation is %d", res);
return 0;
}
C++
// C++ code to illustrate Arguments
#include
using namespace std;
// sum: Function definition
int sum(int a, int b)
{
// returning the addition
return a + b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// sum() is called with
// num1 & num2 as ARGUMENTS.
res = sum(num1, num2);
// Displaying the result
cout << "The summation is " << res;
return 0;
}
C
// C code to illustrate Parameters
#include
// Mult: Function definition
// a and b are the PARAMETERS
int Mult(int a, int b)
{
// returning the multiplication
return a * b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// Mult() is called with
// num1 & num2 as ARGUMENTS.
res = Mult(num1, num2);
// Displaying the result
printf("The multiplication is %d", res);
return 0;
}
C++
// C++ code to illustrate Parameters
#include
using namespace std;
// Mult: Function definition
// a and b are the parameters
int Mult(int a, int b)
{
// returning the multiplication
return a * b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// Mult() is called with
// num1 & num2 as ARGUMENTS.
res = Mult(num1, num2);
// Displaying the result
cout << "The multiplication is " << res;
return 0;
}
输出:
The summation is 30
参数
参数被称为在函数声明或定义期间定义的变量。这些变量用于接收在函数调用期间传递的参数。函数原型中的这些参数在为其定义的函数的执行期间使用。这些也称为形式参数或形式参数。
示例:假设需要定义一个 Mult()函数来将两个数字相乘。这两个数字称为参数,是在定义函数Mult() 时定义的。
C
// C code to illustrate Parameters
#include
// Mult: Function definition
// a and b are the PARAMETERS
int Mult(int a, int b)
{
// returning the multiplication
return a * b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// Mult() is called with
// num1 & num2 as ARGUMENTS.
res = Mult(num1, num2);
// Displaying the result
printf("The multiplication is %d", res);
return 0;
}
C++
// C++ code to illustrate Parameters
#include
using namespace std;
// Mult: Function definition
// a and b are the parameters
int Mult(int a, int b)
{
// returning the multiplication
return a * b;
}
// Driver code
int main()
{
int num1 = 10, num2 = 20, res;
// Mult() is called with
// num1 & num2 as ARGUMENTS.
res = Mult(num1, num2);
// Displaying the result
cout << "The multiplication is " << res;
return 0;
}
输出:
The multiplication is 200
自变量和参数的区别
Argument | Parameter |
---|---|
When a function is called, the values that are passed during the call are called as arguments. | The values which are defined at the time of the function prototype or definition of the function are called as parameters. |
These are used in function call statement to send value from the calling function to the receiving 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 |
Example:
|
Example:
|
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程。