当前位置:编程学习 > C/C++ >>

17_1体会函数参数传递(1)

[cpp] 
#include <iostream>    
using namespace std;    
void jiaohuan(int x, int y);  
int main(void)  
{   www.zzzyk.com
    int  a,b;  
    cin>>a>>b;  
    if (a<b)  
    jiaohuan(a, b);   
    cout<<"a,b="<<a<<","<<b;  
    return 0;  
}  
void jiaohuan(int x, int y)  
{  
    int t;//新定义一个数,用来保存数  
    t=x; //将x保存在t内  
    x=y;   
    y=t;  
}  
//该程序能够实现互换  
<pre class="cpp" name="code">#include <iostream>    
using namespace std;    
void jiaohuan(int *x, int *y);//形式参数为指针  
int main(void)  
{  
    int  a,b;  
    cin>>a>>b;  
    if (a<b)  
        jiaohuan(&a, &b);   
    cout<<"a,b="<<a<<","<<b;  
    return 0;  
}  
void jiaohuan(int *x, int *y)  
{  
    int t;//定义一个新的数  
    t=*x;//将指针x保存在t内  
    *x=*y; //y的指针复制给x  
    *y=t;//指针x赋值给指针y  
}  
//该程序能够实现两数互换</pre><pre class="cpp" name="code"><pre class="cpp" name="code">#include <iostream>    
using namespace std;    
void jiaohuan(int &x, int &y);//形式参数为引用x的值  
int main(void)  
{  
    int  a,b;  
    cin>>a>>b;  
    if (a<b)   
        jiaohuan(a, b); //引用a,b的值  
    cout<<"a,b="<<a<<","<<b;  
    return 0;  
}  
void jiaohuan(int &x, int &y)  
{  
    int t;  
    t=x; //x的值保存在t内  
    x=y; //y的值复制给x  
    y=t;//由t保存的x的值赋值给y  
}  
//该程序能够实现两数互换</pre><br>  
<pre></pre>  
<pre></pre>  
  
</pre>  
补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,