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

c语言 引用调用

Here you will learn about difference between call by value and call by reference in C.

在这里,您将了解C中按值调用和按引用调用之间的区别。

There are two different ways of passing values to functions: call by value and call by reference. They are also called as pass by value and pass by reference.

将值传递给函数的方式有两种:按值调用和按引用调用。 它们也称为按值传递和按引用传递。

在C中按值调用 (Call by Value in C)

In call by value method a copy of actual parameters is passed which is stored in formal parameters.

在按值调用方法中,传递实际参数的副本,该副本存储在形式参数中。

Actual Parameters: The parameters that are passed while calling the function are called actual parameters.

实际参数:调用函数时传递的参数称为实际参数。

Formal Parameters: The parameters that hold the values of actual parameters are called formal parameters.

形式参数:保存实际参数值的参数称为形式参数。

In call by value method, if any changes are done in formal parameters then actual parameters are not changed.

在按值调用方法中,如果对正式参数进行了任何更改,则实际参数不会更改。

Let’s take an example of call by value in C language.

让我们以C语言中的按值调用为例。

Example:

例:

#include<stdio.h>
 
void fun(int x)
{
	x=x+5;
}
 
int main()
{
	int a=10;
	printf("Before calling\na=%d",a);
	fun(a);
	printf("\n\nAfter calling\na=%d",a);
	
	return 0;
}

Output:

输出:

Call by Value in C

As you can clearly see in output that after calling the function and doing changes in formal parameter x, the actual parameter a is not changed.

如您在输出中清楚看到的那样,在调用函数并对形式参数x进行更改之后,实际参数a不会更改。

在C中通过引用调用 (Call by Reference in C)

In call by reference method, reference or address of actual parameters is passed which is stored in formal parameters.

在按引用调用方法中,将传递实际参数的引用或地址,并将其存储在形式参数中。

If any changes are done in formal parameters then actual parameters are also changed.

如果对形式参数进行了任何更改,则实际参数也会更改。

Let’s take an example of call by reference in C language.

让我们以C语言中的引用调用为例。

Example:

例:

#include<stdio.h>
 
void fun(int *x)
{
	*x=(*x)+5;
}
 
int main()
{
	int a=10;
	printf("Before calling\na=%d",a);
	fun(&a);
	printf("\n\nAfter calling\na=%d",a);
	
	return 0;
}

Output:

输出:

Call by Reference in C

You can clearly see in output that after calling the function and doing changes in formal parameter x, the actual parameter a is also changed.

您可以从输出中清楚地看到,在调用函数并对形式参数x进行更改之后,实际参数a也将更改。

Difference between Call by Value and Call by Reference in C

C中按值调用和按引用调用之间的区别 (Difference between Call by Value and Call by Reference in C)

S. No.Call by ValueCall by Reference
1.A copy of actual parameters is passed into formal parameters.Reference of actual parameters is passed into formal parameters.
2.Changes in formal parameters will not result in changes in actual parameters.Changes in formal parameters will result in changes in actual parameters.
3.Separate memory location is allocated for actual and formal parameters.Same memory location is allocated for actual and formal parameters.
序号 按价值致电 通过参考电话
1。 实际参数的副本将传递给形式参数。 实际参数的引用将传递给形式参数。
2。 形式参数的更改不会导致实际参数的更改。 形式参数的更改将导致实际参数的更改。
3。 为实际和形式参数分配了单独的存储位置。 相同的内存位置分配给实际参数和形式参数。

If you have any doubts related to above tutorial then you can ask it by commenting below.

如果您对以上教程有任何疑问,可以在下面评论中提出。

翻译自: https://www.thecrazyprogrammer.com/2015/12/difference-between-call-by-value-and-call-by-reference-in-c.html

c语言 引用调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值