-4

I thought that calling function by value will never work, and I should always use call by reference, but trying this code...

// call by value 
#include<stdio.h>

int Add(int a, int b)
{
    int c = a + b ;
    return c ; 
}

int main()
{
    int x = 2 , y = 4 ;
    int z = Add(x,y);
    printf("%d\n",z); 
}

output will be: 6

it works fine in both ways (call by value & call by reference),

// call by reference
#include<stdio.h>

int Add(int* a, int* b)
{
    int c = *a + *b ;
    return c ; 
}

int main()
{
    int x = 2 , y = 4 ;
    int z = Add(&x,&y);
    printf("%d\n",z); 
}

output will be: 6

not like the famous swap function example - when calling by value it doesn't swap -

// call by value
#include <stdio.h>

void swap(int a, int b)
{
   int temp;

   temp = b;
   b   =  a;
   a   = temp;   
}

int main()
{
   int x = 1 , y = 2;

   printf("x = %d , y = %d\n", x,y); 

   swap(x, y); 

   printf("after swapping\n"); 
   printf("x = %d , y = %d\n", x,y); 

   return 0;
}

.. it only worked calling by reference

// call by reference
#include <stdio.h>

void swap(int *a, int *b)
{
   int temp;

   temp = *b;
   *b   = *a;
   *a   = temp;   
}

int main()
{
   int x = 1 , y = 2;

   printf("x = %d , y = %d\n", x,y); 

   swap(&x, &y); 

   printf("after swapping\n"); 
   printf("x = %d , y = %d\n", x,y); 

   return 0;
}

So How can I judge if "calling by value" going to work or not ?!

Ahmed Safwat
  • 31
  • 2
  • 10

1 Answers1

-1

So How can I judge that call by value method is valid or not ?!

Well, it depends on what your function is about to do.

In your above example, you only need the values of (x,y) for computing, but you never plan to change their value during your function. While call-by-reference will work in this case, it is unneccessary.

In the other (indirectly given) example you obviously want to change two variable's content (that is - swap it). You can access these variables from the main-function in your Swap-function, but how can you make the change persistent? That's only possible by call-by-reference, because you have to write the changed content into a variable that survives the function.

The following will not work:

// call by value 
#include<stdio.h>

void Swap(int a, int b)
{
    int c = a;
    a = b;
    b = c;
    // from here on a, b, c will be destroyed
    // therefore the change cannot be seen outside the function
}

int main()
{
    int x = 2 , y = 4 ;
    Swap(x,y);
    printf("x: %d --- y: %d\n",x,y); 
}

So as a rule to keep in mind:

If you want to make a change that's supposed to survive the function's end, use call-by-reference. If you just work with some data but do not want to (or must not) change their value, use call-by-value.

vonAlenberg
  • 552
  • 1
  • 3
  • 11
  • C does not support call by reference. The question is pointless. – too honest for this site Mar 10 '17 at 09:05
  • Please provide a reference to the C standard where it specifies references. You apparently confuse C and C++. – too honest for this site Mar 10 '17 at 09:12
  • I really don't see your problem. "Call-by-reference" is a _fixed term_ meaning that a function has access to (global) variables through pointers, which don't contain the var's value but their address. So yes, C does support call-by-reference, even if it uses pointers, which are a form of local variables. And no, I don't confuse it with C++ ;) – vonAlenberg Mar 10 '17 at 09:27
  • A pointer is **not** a reference. The term "pass by reference" commonly does not refer to pointers. Pointers are first class types passed by value like any other type in C. References are not. And pointers are not related to local/global (btw: the term "global" is not used in C, neither for scope nor storage duration). – too honest for this site Mar 10 '17 at 09:33