GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, March 11, 2011

CPP - Call By Refrences

#include <iostream>

using namespace std;

// call by reference - reference argument
void addWithTwo(int &);
void addWithTwo(int &ref){
    ref = ref + 2;
}

// call by reference - pointer argument
void addByTwo(int *);
void addByTwo(int *ptr){
    *ptr = *ptr + 2;
}

int main()
{
    int a = 10, b = 11;

    addWithTwo(a);
    addByTwo(&b);

    cout << "Value of a: " << a << endl;
    cout << "Value of b: " << b << endl;
    return 0;
}

Share/Bookmark

No comments:

Post a Comment