C++中的函数用法同C语言的区别不大,出了参数传递可以用引用之外。
参数传递可以是:传值,传指针,传引用。
返回值:各种数据类型,指针,引用。
这里讲一个对指针的引用。
举一个交换两个整形数的例子来说明:
源程序如下:
#include <iostream> using namespace std; void iswapint(int a,int b) //第一个输出结果,变量的值和地址没有任何变化,传递的是变量的一个拷贝。 { int t=a; a=b; b=t; } void iswapref(int &a,int &b)//使用引用,直接对变量操作,地址没有变化,变量的数值变化了。 { int t=a; a=b; b=t; } void iswapptr1(int *a,int *b)//使用指针传递,里面的方法,*a等,其实这个叫做间接引用指针。 { int t=*a; *a=*b; *b=t; } void iswapptr2(int *a,int *b)//指针传递,同第一个一样,传递的是指针的拷贝,对原来的变量和地址都没有改变。 { int *t=a; a=b; b=t; } void iswaptrref1(int *&a,int *&b)//变量的值没有改变,但是改变了变量的地址,因此通过地址找变量的数值就不一样了 //这个相当于有两个房子A和B在广州和香港,里面对应的是一个男人(Y)和一个女人(X),把广州和香港对调,地址变了 //但是A房子还是Y男,B房子还是X女,如果通过地址找则香港是Y男了,而广州是X女了。 { int *t=a; a=b; b=t; } void iswaptrref2(int *&a,int *&b)//这个结果是因为上面的函数原因造成的,其实这个跟void iswapptr1(int *a,int *b)//使用指针传递,里面的方法,*a等,其实这个叫做间接引用指针。 { int t=*a; *a=*b; *b=t; } int main() { int x=5,y=9; int *ptx=&x; int *pty=&y; cout<<"Original value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; iswapint(x,y); //iswapref(x,y); //iswapptr1(ptx,pty); //iswapptr2(ptx,pty); //iswaptrref1(ptx,pty); //iswaptrref2(ptx,pty); cout<<endl; cout<<"Changed value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; cout<<"Original value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; //iswapint(x,y); iswapref(x,y); //iswapptr1(ptx,pty); //iswapptr2(ptx,pty); //iswaptrref1(ptx,pty); //iswaptrref2(ptx,pty); cout<<endl; cout<<"Changed value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; cout<<"Original value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; //iswapint(x,y); //iswapref(x,y); iswapptr1(ptx,pty); //iswapptr2(ptx,pty); //iswaptrref1(ptx,pty); //iswaptrref2(ptx,pty); cout<<endl; cout<<"Changed value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; cout<<"Original value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; //iswapint(x,y); //iswapref(x,y); //iswapptr1(ptx,pty); iswapptr2(ptx,pty); //iswaptrref1(ptx,pty); //iswaptrref2(ptx,pty); cout<<endl; cout<<"Changed value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; cout<<"Original value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; //iswapint(x,y); //iswapref(x,y); //iswapptr1(ptx,pty); //iswapptr2(ptx,pty); iswaptrref1(ptx,pty); //iswaptrref2(ptx,pty); cout<<endl; cout<<"Changed value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; cout<<"Original value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; //iswapint(x,y); //iswapref(x,y); //iswapptr1(ptx,pty); //iswapptr2(ptx,pty); //iswaptrref1(ptx,pty); iswaptrref2(ptx,pty); cout<<endl; cout<<"Changed value: x= "<<x<<" y= "<<y<<endl; cout<<"*ptx= "<<*ptx<<" *pty= "<<*pty<<endl; cout<<"Address: x="<<ptx<<" y= "<<pty<<endl; return 0; }输出结果:
Function is: swap(int ,int)
Original value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Changed value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Function is: swap(int &,int &) Original value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Changed value: x= 9 y= 5 *ptx= 9 *pty= 5 Address: x=0xbfd9b448 y= 0xbfd9b444 Function is: swap(int *,int *) Original value: x= 9 y= 5 *ptx= 9 *pty= 5 Address: x=0xbfd9b448 y= 0xbfd9b444 Changed value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Function is: swap(int *,int *) Original value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Changed value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Function is: swap(int *&,int *&) Original value: x= 5 y= 9 *ptx= 5 *pty= 9 Address: x=0xbfd9b448 y= 0xbfd9b444 Changed value: x= 5 y= 9 *ptx= 9 *pty= 5 Address: x=0xbfd9b444 y= 0xbfd9b448 Function is: swap(int *&,int *&) Original value: x= 5 y= 9 *ptx= 9 *pty= 5 Address: x=0xbfd9b444 y= 0xbfd9b448 Changed value: x= 9 y= 5 *ptx= 5 *pty= 9 Address: x=0xbfd9b444 y= 0xbfd9b448