Getting different results with passing by value and passing by reference

3

I'm testing these code examples in C:

Call by value

#include <stdio.h>

/* function definition to swap the values */
void swap(int x, int y) {

   int temp;

   temp = x; /* save the value of x */
   x = y;    /* put y into x */
   y = temp; /* put temp into y */

   return;
}

int main () {

   /* local variable definition */
   int a = 100;
   int b = 200;

   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );

   /* calling a function to swap the values */
   swap(a, b);

   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}

Output:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200

So far so good, now the output of the second example is intriguing me:

Call by reference

#include <stdio.h>

/* function definition to swap the values */
void swap(int *x, int *y) {

   int temp;
   temp = *x;    /* save the value at address x */
   *x = *y;      /* put y into x */
   *y = temp;    /* put temp into y */

   return;
}

int main () {

   /* local variable definition */
   int a = 100;
   int b = 200;

   printf("Before swap, value of a : %d\n", a );
   printf("Before swap, value of b : %d\n", b );

   /* calling a function to swap the values.
      * &a indicates pointer to a ie. address of variable a and 
      * &b indicates pointer to b ie. address of variable b.
   */
   swap(&a, &b);

   printf("After swap, value of a : %d\n", a );
   printf("After swap, value of b : %d\n", b );

   return 0;
}

Output:

Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200 # aqui era pra ser 100
After swap, value of b :100 # e aqui era pra ser 200

Why are values reversed?

    
asked by anonymous 25.05.2016 / 03:45

1 answer

5

The swap() function does what is expected of a swap function, so the second code is right and the first one is wrong, after all in the first no exchange is made. Of course, the goal of the first one was to show that the value is isolated, albeit a poor example.

When you pass the value, the parameter has no direct relation to the variables that originated the argument. What is passed is copied to the function, any changes made to these values within the function are only valid therein. The function used the variables used as arguments in the function call are with their values intact there.

When passing by reference, what is passing is the address of a value, possibly the address of a variable (as it is in this case). So any change in this value affects the original location where it was, so when leaving the function the value will change.

When a parameter is a pointer, what is copied is the memory address of something. Any manipulation of this location has visibility throughout the application. This is the basis of what we call "passage by reference." The pointer serves precisely to create an indirection, so when you access a value directly, access is done indirectly through the address contained in it. It's a kind of envelope.

When you pass a normal value, you are making a photocopy of a letter. Any change in this copy does not reflect the original letter. When you use the pointer, it is an envelope that contains the letter. So any change in the letter is "final".

25.05.2016 / 04:22