函数 传值调用 指针调用 引用调用

#include <iostream>
using namespace std;

int max(int num1, int num2);
void swap(int *x, int *y);
void swap(int &x, int &y);

int main(){
    cout << "hello world" << endl;
    int num1 = 100;
    int num2 = 200;

    int v_max = max(num1, num2);
    cout << v_max << endl;

    //cout << "before swap " << num1 << " " << num2 << endl;
    //swap(&num1, &num2);
    //cout << "after swap " << num1 << " " << num2 << endl;

    cout << "before swap " << num1 << " " << num2 << endl;
    swap(num1, num2);
    cout << "after swap " << num1 << " " << num2 << endl;


}


int max(int num1, int num2)
{

    if (num1 > num2)
        return num1;
    else
        return num2;
} 

void swap(int *x, int *y)
{
    int temp;
    temp = *x; /*保存指针*/
    *x = *y;
    *y = temp;

    return;
}

void swap(int &x, int &y)
{
    int tmp;
    tmp = x;
    x = y;
    y = tmp;

    return;
}

 

posted on 2015-03-19 17:09  雨渐渐  阅读(257)  评论(0编辑  收藏  举报

导航