c语言 引用调用_在C中按值调用和按引用调用

c语言 引用调用

To understand this tutorial you must have knowledge of pointers in C. So read previous article about pointers:  Pointers in C Programming

Now armed with the knowledge of pointers lets move our quest to learn C programming one step forward. Today I will tell you about the second advance feature of functions i.e. call by value and call by reference in C. Knowingly or unknowingly we have used the call by value feature in many programs till now. So it will be easy to understand the logic behind it. On the other side call by reference is used with pointers.

Call by Value in C

In previous tutorials I told you about the actual and formal arguments (parameters). In this method, whenever we make changes in formal arguments then it doesn’t change the value in actual arguments. As its name suggests, call by value feature means calling the function and passing some values in it. The values of actual arguments are copied into formal arguments. We have done it so many times till now. Lets take a fresh example to understand it quickly.

Output

Call by Value in C

Explanation

  • The program code is almost self-explanatory. First I have printed the value of variable a before calling of function and then I have passed one argument in the function fun().
  • The value of variable a of main() is copied into variable a of function fun().
  • Now I have changed the value of variable a by adding 5 in it. After that I have printed the variable a in function fun(). 
  • At last the value of variable a is again printed in main(). You can observe in output that the change that was done in variable a in function fun() is not affected the value of variable a in main().

Call by Reference in C

As I told earlier while applying call by value feature of functions we cannot change the values in actual arguments by changing the values in formal arguments. Many times we stick in a condition when we need to change the values of actual arguments in any other function. In those cases we use call by reference method.

In this method we will send the address of variables to the function. Any changes in formal arguments causes change in actual arguments. Lets build a simple program to understand this topic.

Output

Call by Reference in C

Explanation

  • In the beginning I have declared the function fun() with its argument as integer pointer.
  • In the function call I have passed the address of variable a instead of passing its value.
  • Now I have the address in pointer variable a of function fun().
  • Then I have done some changes in the pointer variable and printed its value. Finally the variable a is again printed in main()
  • You can see in the output that the change done in the pointer variable a of function fun() causes change in variable a of main(). 

You may face difficulty in understanding the concept of call by reference. You can ask your queries by commenting below.

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

c语言 引用调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值