1. 程式人生 > >函式引數的傳遞

函式引數的傳遞

 

1.函式的引用傳遞

void Exchg2(int &x, int &y)
{
 int tmp=x;
 x=y;
 y=tmp;
 printf("x=%d,y=%d\n",x,y);  //6 4
}
void main()
{
 int a=4;
 int b=6;
 Exchg2(a,b);
 printf("a=%d,b=%d\n", a, b);  //6  4
}

2.函式的地址傳遞

Exchg2(int *px, int *py)
{
 int tmp=*px;
 *px=*py;
 *py=tmp;
 printf("*px=%d,*py=%d\n",*px,*py);  //6  4
}
main()
{
 int a=4;
 int b=6;
 Exchg2(&a,&b);
 print("a=%d,b=%d\n", a, b);  //6   4
}

3.函式的值傳遞

void Exchg1(int x, int y) 
{
 int tmp;
 tmp=x;
 x=y;
 y=tmp;
 printf("x=%d,y=%d\n",x,y);  //6  4
}
void main()
{
 int a=4,b=6;
 Exchg1 (a,b) ;
 printf("a=%d,b=%d\n",a,b);   //4   6
}