Return to Topic Menu | Computer Science Main Page | MathBits.com  | Terms of Use | Resource CD

 

Pointers and Functions

Remember, functions can return only ONE value. Until now it was impossible to change two different non-array values within a function and return both values to main( ). 

By Reference:
We can now swap two variables, by using the addresses of the variables from within the function.  When the function reverses the values, main( ) will also recognize the changes. 

#include <iostream.h>

void swap(int &num1, int &num2);

int main(void)
{
     int a=10,bj=20;
     cout<< "Before the swap, a is " << a << " and b is " << b << "."<< endl;
     swap(a,b);
     cout<< "After the swap, a is " << a << " and b is " << b << "."<< endl;
     return 0;
}

//-------------- Function to reverse two values ----------------------------------------
void swap(int &num1, int &num2)    // receiving addresses not values
{
     int temp;     // temporary holding variable

     temp = num1;
     num1 = num2;
     num2 = temp;

     return;     // main( )'s variables, not copies of them, have been changed
}

By Pointers:
Let's try this same swapping process using pointers instead of addresses.


#include <iostream.h>
void p_swap(int *pNum1, int *pNum2);
int main(void)
{
     int a=10, b=20;
     cout<< "Before the swap, a is " << a << " and b is " << b << "."<< endl;
     p_swap(&a, &b);    //pass the addresses
     cout<< "After the swap, a is " << a << " and b is " << b << "."<< endl;
     return 0;
}

//-------------- Function to reverse two values -----------------------------------------
void p_swap(int *pNum1, int *pNum2)    // receiving addresses not values
{
     int temp;    // temporary holding variable

     temp = *pNum1;    // swap the values stored at the addresses
     *pNum1 = *pNum2;
     *pNum2 = temp;

     return;     // main( )'s variables, not copies of them, have been changed
}