Obtaining different results by passing by value and passing by reference

Asked

Viewed 85 times

3

I’m testing these examples of C codes:

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;
}

Exit:

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 Ference

#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;
}

Exit:

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 the figures are coming out reversed?

1 answer

5


The function swap() does what is expected of an exchange function, then the second code is right and the first is wrong, after all in the first no exchange is made. Of course, probably the purpose of the former was to show that the value is isolated, although a bad example.

When the value is passed, 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 inside. Ending the function the variables used as arguments in the function call are with their values intact there.

When you go by reference, what you’re going through is the address of a value, possibly the address of a variable, as it happens in this case. Then any change in this value affects the original location where it was, so when leaving the function the value will be changed.

When a parameter is a pointer, what is copied is the memory address of something. Any manipulation of this site has visibility throughout the application. This is the basis of what we call "passing by reference". The pointer serves precisely to create a indirect, then to access a value directly, the access is done indirectly through the address contained in it. It is a kind of envelope.

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

  • then in the second case he did the memory address change that? + 1

  • 1

    @Nikobellic No, it changes the value as in the first example. The difference is the life time of the change. In the first, the change exists only within the function, in the second it is made directly in the variable of the main function. This example of swap() is terrible. These internet courses may even give a foundation, but they teach a lot of wrong. Then I put what would be a function swap() better do what you say.

  • On second thought, I’m not going for another function because the other function has nothing useful for the learning that was tried to pass there. It’s a completely different matter.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.