社会十句经典大实话
2007-10-04 10:16
288 查看
一、值传递
#include "stdafx.h"
#include<iostream>
using namespace std;
//将两个数交换值
void swap(int a,int b){
int t=a;
a=b;
b=t;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x=1,y=2;
cout<<"x="<<x<<" y="<<y<<endl;
swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
return 0;
}
二、引用传递
#include "stdafx.h"
#include<iostream>
using namespace std;
//将两个数交换值
void swap(int &a,int &b){ //&a 、&b为实参的别名
int t=a;
a=b;
b=t;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x=1,y=2;
cout<<"x="<<x<<" y="<<y<<endl;
swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
return 0;
}
总结:
值传递中,形参是形参,实参是实参;
引用传递中,形参等于实参。
#include "stdafx.h"
#include<iostream>
using namespace std;
//将两个数交换值
void swap(int a,int b){
int t=a;
a=b;
b=t;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x=1,y=2;
cout<<"x="<<x<<" y="<<y<<endl;
swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
return 0;
}
二、引用传递
#include "stdafx.h"
#include<iostream>
using namespace std;
//将两个数交换值
void swap(int &a,int &b){ //&a 、&b为实参的别名
int t=a;
a=b;
b=t;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x=1,y=2;
cout<<"x="<<x<<" y="<<y<<endl;
swap(x,y);
cout<<"x="<<x<<" y="<<y<<endl;
return 0;
}
总结:
值传递中,形参是形参,实参是实参;
引用传递中,形参等于实参。