1.传值调用

#include<stdio.h>

void swap(int x, int y)

{

int tmp = x;

x = y;

y = tmp;

}

int main(void)

{

int a = 1, b = 2;

printf("a=%d, b=%d\n", a, b);

swap(a, b);

printf("a=%d, b=%d\n", a, b);

system("pause");

return 0;

}

a=1, b=2

a=1, b=2

2.传址调用

#include<stdio.h>

void swap(int *x, int *y)

{

int tmp = *x;

*x = *y;

*y = tmp;

}

int main(void)

{

int a = 1, b = 2;

printf("a=%d, b=%d\n", a, b);

swap(&a, &b);

printf("a=%d, b=%d\n", a, b);

system("pause");

return 0;

}

a=1, b=2

a=2, b=1

3.引用调用

#include<stdio.h>

void swap(int &x, int &y)

{

int tmp = x;

x = y;

y = tmp;

}

int main(void)

{

int a = 1, b = 2;

printf("a=%d, b=%d\n", a, b);

swap(a, b);

printf("a=%d, b=%d\n", a, b);

system("pause");

return 0;

}

#include<iostream>

using namespace std;

void swap(int &x, int &y)

{

int tmp = x;

x = y;

y = tmp;

}

int main(void)

{

int a = 1, b = 2;

printf("a=%d, b=%d\n", a, b);

swap(a, b);

printf("a=%d, b=%d\n", a, b);

system("pause");

return 0;

}

a=1, b=2

a=2, b=1