函数实参和形参

The terms parameter and argument are sometimes loosly used interchangeably, and the context is used to distinguish the meaning.

The terms parameter(sometimes called formal paramter) is often used to refer to the variable as found in the function definition, while argument(sometimes called actual parameter) refers to the actual input passed.

For example, if one defines a function as def f(x):..., then x is the terms parameter, while if it called by a = ...; f(a) then a is the argument.

 

形参出现在函数定义中,在整个函数体内都可以使用,离开该函数则不能使用。

实参出现在主调函数中,进入被调函数后,实参变量就不能使用了。

实参和形参的功能是作数据传送,发生函数调用时,主调函数把实参的值传送给被调函数的形参,从而实现主调函数向被调函数的数据传送。

 

函数的形参和实参具有以下特点:

1. 形参变量只有在被调用时才分配内存单元,在调用结束时,即可释放所分配的内存单元。

    因而,形参只有在函数内部有效。函数调用结束后返回主调函数后则不能再使用该形参变量。

2. 实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须具有确定的值,以便把这些值传送给形参。

    因此应预先用赋值、输入等办法使实参获得确定值。

3. 实参和形参在数量上、类型上、顺序上应严格一致,否则会发生类型不匹配的错误。

4. 函数调用中发生的数据传送是单向的。

    即 只能把实参的值传送给形参,而不能把形参的值反向传送给实参。

    因此 在函数调用过程中,形参的值发生改变,而实参中的值不会变化。

 

Reference:

1. https://en.wikipedia.org/wiki/Parameter_(computer_programming)#Parameters_and_arguments

2. http://blog.csdn.net/quannii/article/details/8469116

转载于:https://www.cnblogs.com/azureflu/p/4149856.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值