📜  功能编程-按引用调用

📅  最后修改于: 2021-01-07 05:15:49             🧑  作者: Mango


在“按引用调用”中,原始值会更改,因为我们传递了参数的引用地址。实际的和正式的参数共享相同的地址空间,所以在函数内部任何有价值的变化反映内部以及以外的函数。

在C++中通过引用调用

以下程序显示了按值调用在C++中的工作方式-

#include  
using namespace std; 

void swap(int *a, int *b) {    
   int temp; 
   temp = *a; 
   *a = *b; 
   *b = temp; 
   cout<

它将产生以下输出-

value of a before sending to function:  50 
value of b before sending to function:  75 
value of a inside the function:  75 
value of b inside the function: 50 
value of a after sending to function:  75 
value of b after sending to function:  50 

在Python按引用调用

以下程序显示了按值调用在Python-

现场演示

def swap(a,b): 
   t = a; 
   a = b; 
   b = t; 
   print "value of a inside the function: :",a 
   print "value of b inside the function: ",b 
   return(a,b) 
    
# Now we can call swap function 
a = 50 
b =75 
print "value of a before sending to function: ",a 
print "value of b before sending to function: ",b 
x = swap(a,b) 
print "value of a after sending to function: ", x[0] 
print "value of b after sending to function: ",x[1] 

它将产生以下输出-

value of a before sending to function:  50 
value of b before sending to function:  75 
value of a inside the function:  75 
value of b inside the function:  50 
value of a after sending to function:  75 
value of b after sending to function:  50