首页 > 代码库 > Parameter Transmission

Parameter Transmission

在C++中,函数参数传递有3种方式,分别是按值传递、按地址传递和按引用传递。其测试代码如下:

 1 //Parameter Transmission in C++ 2 #include<iostream> 3  using namespace std; 4  5  void swap_value(int a,int b){     //交换形参a、b的数值 6     7    cout<<"a="<<a<<" b="<<b<<endl;      8    int temp=a; 9        a=b;10        b=temp;11 12    cout<<"After the function is called:"<<endl;13    cout<<"a="<<a<<" b="<<b<<endl;    14  }15 16  void swap_reference(int &a,int &b){     //交换形参a、b的引用17    cout<<"a="<<a<<" b="<<b<<endl;     18    19    int temp=a;20        a=b;21        b=temp;22 23    cout<<"After the function is called:"<<endl;24    cout<<"a="<<a<<" b="<<b<<endl;     25  }26 27  void swap_pointer(int *a,int *b){     //交换形参a、b的地址28    cout<<"a="<<*a<<" b="<<*b<<endl;     29    30    int  temp=*a;31         *a=*b;32         *b=temp;33  34    cout<<"After the function is called:"<<endl;35    cout<<"a="<<*a<<" b="<<*b<<endl;     36  }37 38  void main(void){39    40    int x1=100,y1=200; 41    cout<<"The result of passing by value is: "<<endl;42    cout<<"Before the function is called:"<<endl;43    cout<<"(x1="<<x1<<",y1="<<y1<<")\n";44    swap_value(x1,y1);45    cout<<"(x1="<<x1<<",y1="<<y1<<")\n\n";46   47    int x2=100,y2=200; 48    cout<<"The result of passing by reference is: "<<endl;49    cout<<"Before the function is called:"<<endl;50    cout<<"(x2="<<x2<<",y2="<<y2<<")\n";51    swap_reference(x2,y2);52    cout<<"(x2="<<x2<<",y2="<<y2<<")\n\n";53 54    int x3=100,y3=200; 55    cout<<"The result of passing by pointer is: "<<endl;56    cout<<"Before the function is called:"<<endl;57    cout<<"(x3="<<x3<<",y3="<<y3<<")\n";58    swap_pointer(&x3,&y3);59    cout<<"(x3="<<x3<<",y3="<<y3<<")\n";         60 61 }62  //The screen display are following63  /*The result of passing by value is:64    Before the function is called:65    (x1=100,y1=200)66    a=100 b=20067    After the function is called:68    a=200 b=10069    (x1=100,y1=200)70 71    The result of passing by reference is:72    Before the function is called:73    (x2=100,y2=200)74    a=100 b=20075    After the function is called:76    a=200 b=10077    (x2=200,y2=100)78 79    The result of passing by pointer is:80    Before the function is called:81    (x3=100,y3=200)82    a=100 b=20083    After the function is called:84    a=200 b=10085    (x3=200,y3=100)86  */

简单解释一下,按值传递很容易理解,实参与形参分配到不同单元;不同单元内容交换,实参原值并没有发生改变。由于C++中有指针和引用这2种特殊的数据类型,故根据它们的定义我们可以知道,无论是按地址传递还是按引用传递,其本质是一样的,都是建立实参与形参地址上统一的基础之上的(即,对应实参的地址与形参的地址是一样的),从而可以实现改变实参内容。

 

在JAVA中,函数参数传递表面上有两种,实际上都将归于一种---按值传递。其测试代码如下:

 1 package test; 2 //Parameter Transmission in JAVA 3 public class Test_One { 4     public static void testint(int xx){xx=10;} 5     public static int returnint(int zz){zz=20;  return zz;} 6     public static void testdouble(double yy){ yy=2.5;} 7     public static void testString(String aa){aa="yang";} 8  9     public static class Object{ int temp=6;}10     public static void testStringArray1(String [] cc){cc[0]="987456";}11     public static void testStringArray2(String [] ee){12         String [] a=new String [20];13         a=ee;14     }15     public static void testObject(Object dd){dd.temp++;}16     17     public static void main(String [] args){18         int x=0;19         System.out.println("x="+x);20         testint(x);21         System.out.println("x="+x);22         23         int z=0;24         System.out.println("z="+z);25         z=returnint(z);26         System.out.println("z="+z);27         28         double y=5.4;29         System.out.println("y="+y);30         testdouble(y);31         System.out.println("y="+y);32         33         String name="wang";34         System.out.println("name="+name);35         testString(name);36         System.out.println("name="+name);37 //----------------------------------------------------------------38         String [] sequence1={"123456"};39         System.out.println("sequence1[0]="+sequence1[0]);40         testStringArray1(sequence1);41         System.out.println("sequence1[0]="+sequence1[0]);42         43         String [] sequence2={"123456"};44         System.out.println("sequence2[0]="+sequence2[0]);45         testStringArray2(sequence2);46         System.out.println("sequence2[0]="+sequence2[0]);47         48         Object de=new Object();49         System.out.println("de.temp="+de.temp);50         testObject(de);51         System.out.println("de.temp="+de.temp);    52         }53 }54 //The screen display are following55 /*x=056   x=057   z=058   z=2059   y=5.460   y=5.461   name=wang62   name=wang63   sequence1[0]=12345664   sequence1[0]=98745665   sequence2[0]=12345666   sequence2[0]=12345667   de.temp=668   de.temp=7 69  */

总结:0.表面上看,可得出如下结论:对于形参是基本数据类型的,函数参数传递是按值传递(即对形参的修改并不影响实参的结果);

           对于形参是对象类型的, 函数参数传递是按引用传递(即对形参的修改直接影响实参的结果)。而实际上它们都是按值传递。

         1.对于参数是基本数据类型,一般而言,形参到实参是单向的;我们若要实现形参与实参双向传递,可通过返回值方法来实现。(见上述的x和z)
            对于参数是对象类型,一般而言,形参到实参是双向的;我们若要实现单向,可通过在方法内部创建新对象,并且实参先传给形参,形参再传给
            新对象。

具体的解释请参考“Parameter passing in Java - by reference or by value?”里面写的非常详细。

下面给出其网页链接http://www.yoda.arachsys.com/java/passing.html