c/c++(const)(5)
函数参数传递
一,按值传递
#include<iostream> using namespace std; void swap(int x ,int y); int main() { int x=5;y=10; cout<<"main before swap,x:"<<x<<"y:"<<y<<endls; swap(x,y); cout<<"main after swap,x:"<<x<<"y:"<<y<<endls; retuen 0; } void swap(int x,int y) { int tmp; cout<<"swap before swap,x:"<<x<<"y:"<<y<<endls; temp=x; x=y; y=temp; cout<<"swap before swap,x:"<<x<<"y:"<<y<<endls; }
二,按指针传递
#include<iostream> using namespace std; void swap(int *x ,int *y); int main() { int x=5;y=10; cout<<"main before swap,x:"<<x<<"y:"<<y<<endls; swap(&x,&y); cout<<"main after swap,x:"<<x<<"y:"<<y<<endls; retuen 0; } void swap(int *px,int *py) { int tmp; cout<<"swap before swap,*px:"<<px<<"*py:"<<py<<endls; temp=*px; *px=*py; *py=temp; cout<<"swap before swap,*px:"<<*px<<"*py:"<<py<<endls; }
三,引用传递参数
#include<iostream> using namespace std; void swap(int &x ,int &y); int main() { int x=5;y=10; cout<<"main before swap,x:"<<x<<"y:"<<y<<endls; swap(x,y); cout<<"main after swap,x:"<<x<<"y:"<<y<<endls; retuen 0; } void swap(int &rx,int &ry) { int tmp; cout<<"swap before swap,rx:"<<rx<<"ry:"<<ry<<endls; temp=rx; rx=ry; ry=temp; cout<<"swap before swap,rx:"<<rx<<"ry:"<<ry<<endls; }
posted on 2013-07-30 16:52 Landscape-Mi 阅读(155) 评论(0) 收藏 举报
浙公网安备 33010602011771号