📜  C / C++中的参数和参数之间的差异以及示例

📅  最后修改于: 2021-05-26 03:19:50             🧑  作者: Mango

争论

参数是指调用函数时在函数内传递的值。这些值通常是在执行过程中需要参数的函数的来源。这些值将分配给所调用函数的定义中的变量。函数传递的值的类型与函数定义中定义的变量的类型相同。这些也称为实际参数实际参数

示例:假设需要用两个数字相加来调用sum()函数。这两个数字称为自变量,并在从其他位置调用时传递给sum()。

C
// C code to illustrate Arguments
  
#include 
  
// sum: Function defintion
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 defintion
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 defintion
// 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 defintion
// 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 defintion
// 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 defintion
// 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:
int num = 20;
Call(num)
  
    // num is argument
Example:
int Call(int rnum)
{
    printf("the num is %d", rnum);
}
  
// rnum is parameter
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”