Home >Backend Development >C#.Net Tutorial >What are the three ways to call functions in C language?

What are the three ways to call functions in C language?

青灯夜游
青灯夜游Original
2019-03-01 13:54:4556411browse

The three ways to call functions in the C language are: 1. Call by value, which is to pass the value of a variable to the formal parameter of the function; 2. Call by reference, which is to pass the address of the variable directly into the function ; 3. Calling by address is implemented through pointers, passing the reference of the variable into the function.

What are the three ways to call functions in C language?

#The operating environment of this article: Windows 7 system, version C11, Dell G3 computer.

The three ways of calling functions in C language are: calling by value, calling by reference and calling by address.

1. Call by value

Call by value is to pass the value of a variable to the formal parameter of the function. In fact, it is to use the value of the variable to generate a new Formal parameters, the value is returned after the formal parameter operation.

The values ​​changed in the function cannot affect the outside world, so changes to the formal parameters in the function will not affect the values ​​of variables outside the function.

Let’s understand it through the code example:

#include<stdio.h>  
void myswap(int x, int y)  
{  
    int t;  
    t=x;  
    x=y;  
    y=t;  
}  
int main()  
{  
    int a, b;  
    printf("请输入待交换的两个整数:");  
    scanf("%d %d", &a, &b);  
    myswap(a,b);  //作为对比,直接交换两个整数,显然不行  
    printf("调用交换函数后的结果是:%d 和 %d\n", a, b);  
    return 0;  
}

Run:

What are the three ways to call functions in C language?

##[Video tutorial recommendation:

C language tutorial]

Analysis:

The reason for no successful interaction is: using call by value, only the value is changed on the formal parameters, not the actual parameters.

2. Call by address

Call by address is to pass the address of the variable directly into the function. It assigns the address of a variable to Give the pointer to the formal parameter in the function so that the pointer points to the address of the real variable, because changes to the content of the address pointed to by the pointer can be reflected outside the function, that is, the value of the variable outside the function can be changed.

Let’s understand it through code examples:

#include<stdio.h>  
void myswap(int *p1, int *p2)  
{  
    int  t;  
    t=*p1;  
    *p1=*p2;  
    *p2=t;  
}  
int main()  
{  
    int a, b;  
    printf("请输入待交换的两个整数:");  
    scanf("%d %d", &a, &b);  
    myswap(&a,&b);  //交换两个整数的地址  
    printf("调用交换函数后的结果是:%d 和 %d\n", a, b);  
    return 0;  
}

Run:

What are the three ways to call functions in C language?

Analysis:

The reason for successful interaction Yes: Using the call by address, the original address is changed using the pointer, so the actual parameters are exchanged.

3. Reference call

#Reference call is actually implemented through pointers. The reference of the variable is passed into the function; the usage address can be reached The effect of calling, but when used like calling by value, the function can modify its value.

Let’s understand it through code examples:

#include<stdio.h>
void myswap(int &x, int &y)
{
    int t;
    t=x;
    x=y;
    y=t;
}
  
int main()
{  
    int a, b;
    printf("请输入待交换的两个整数:");
    scanf("%d %d", &a, &b);
    myswap(a,b);  //注意:这里调用方式与传值调用一样,直接以变量a和b作为实参交换
    printf("调用交换函数后的结果是:%d 和 %d\n", a, b);
    return 0;
}

Run:

What are the three ways to call functions in C language?

Analysis:

The reason for successful interaction Yes: Call by reference. The reference directly changes the values ​​of the two actual parameter variables a and b, so they are exchanged.

The above is the detailed content of What are the three ways to call functions in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn