📜  C中按值调用和按引用调用

📅  最后修改于: 2020-10-22 01:17:16             🧑  作者: Mango

在C中按值调用和按引用调用

有两种方法可以用C语言将数据传递到函数中,即按值调用和按引用调用。

让我们一一理解c语言中的按值调用和按引用调用。

按C中的值调用

  • 在按值调用方法中,实际参数的值将复制到形式参数中。换句话说,可以说在按值调用方法中的函数调用中使用了变量的值。
  • 在按值调用方法中,我们不能通过形式参数来修改实际参数的值。
  • 在按值调用中,由于实际参数的值已复制到形式参数中,因此为实际参数和形式参数分配了不同的内存。
  • 实际参数是在函数调用中使用的参数,而形式参数是在函数定义中使用的参数。

让我们尝试通过以下示例理解c语言中按值调用的概念:

#include
void change(int num) {  
    printf("Before adding value inside function num=%d \n",num);  
    num=num+100;  
    printf("After adding value inside function num=%d \n", num);  
}  
int main() {  
    int x=100;  
    printf("Before function call x=%d \n", x);  
    change(x);//passing value in function  
    printf("After function call x=%d \n", x);  
return 0;
}  

输出量

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=100

按值调用示例:交换两个变量的值

#include 
void swap(int , int); //prototype of the function 
int main()
{
    int a = 10;
    int b = 20; 
    printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
    swap(a,b);
    printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20
}
void swap (int a, int b)
{
    int temp; 
    temp = a;
    a=b;
    b=temp;
    printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10 
}

输出量

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20  

在C中通过引用调用

  • 在按引用调用中,变量的地址作为实际参数传递到函数调用中。
  • 由于传递了实际参数的地址,因此可以通过更改形式参数来修改实际参数的值。
  • 通过引用调用,形式参数和实际参数的内存分配都相似。函数中的所有操作都对存储在实际参数地址处的值执行,修改后的值将存储在同一地址。

请参考以下示例进行调用。

#include
void change(int *num) {  
    printf("Before adding value inside function num=%d \n",*num);  
    (*num) += 100;  
    printf("After adding value inside function num=%d \n", *num);  
}    
int main() {  
    int x=100;  
    printf("Before function call x=%d \n", x);  
    change(&x);//passing reference in function  
    printf("After function call x=%d \n", x);  
return 0;
}  

输出量

Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200

通过引用调用示例:交换两个变量的值

#include 
void swap(int *, int *); //prototype of the function 
int main()
{
    int a = 10;
    int b = 20; 
    printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main
    swap(&a,&b);
    printf("After swapping values in main a = %d, b = %d\n",a,b); // The values of actual parameters do change in call by reference, a = 10, b = 20
}
void swap (int *a, int *b)
{
    int temp; 
    temp = *a;
    *a=*b;
    *b=temp;
    printf("After swapping values in function a = %d, b = %d\n",*a,*b); // Formal parameters, a = 20, b = 10 
}

输出量

Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10  

c中按值调用和按引用调用之间的区别

No. Call by value Call by reference
1 A copy of the value is passed into the function An address of value is passed into the function
2 Changes made inside the function is limited to the function only. The values of the actual parameters do not change by changing the formal parameters. Changes made inside the function validate outside of the function also. The values of the actual parameters do change by changing the formal parameters.
3 Actual and formal arguments are created at the different memory location Actual and formal arguments are created at the same memory location