11

What is value semantics and reference semantics and what is the difference between them? Can you please show me with an example in c.

I guess in reference semantics that you just send an pointer to another function then it is reference semantics? I find it hard to grasp what value semantics is? If I only use an int as an argument and then let say return an int from that function then the function uses value semantics? And how does side effects affect this? There must be other examples of value semantics then I mentioned if I were right about it. Can you please give me examples of that. If a function takes a pointer as argument and the functions return value is an int, does the function make use of both reference- and value semantics?

user2682811
  • 175
  • 1
  • 2
  • 5
  • [see here](http://stackoverflow.com/questions/9940643/benefit-of-passing-an-int-by-reference-vs-by-value) perhaps – M.M Nov 23 '14 at 00:09
  • 2
    Value semantic means that you access a copy, while reference semantic means that you access the actual thing. The closest thing to reference semantic is passing an array to a function or passing a pointer. – Sergey Kalinichenko Nov 23 '14 at 00:09
  • Does this answer your question? [What do ‘value semantics’ and ‘pointer semantics’ mean?](https://stackoverflow.com/questions/166033/what-do-value-semantics-and-pointer-semantics-mean) – Karl Knechtel Jul 14 '23 at 15:18

1 Answers1

9

In the reference semantic, an argument refers to the original object, being it for reading or for writing.

In the value semantic, an argument is just the value of an object, i.e. a copy instead of the original. Of course, if you alter this copy with some side effects, the original element remains unchanged.

Example of passing by value:

int f(int a)   /* argument a is passed by value (local variable containing a copy)  */ 
{
    a++;      /* increments the local variable */
    return (a+5);   /* return a value */  
}

int main (int ac, char**av) {
    int b=7, c; 
    c = f(b);  /* b will be copied. The original value is unchanged */
    printf ("b=%d c=%d\n", b, c);  /* prints 7 and 13 */
}

Example of passing by reference:

int fr(int* pa)   /* argument pa is a pointer refering to original value  */ 
{
    *pa+=1;      /* increments value pointed to (the original variable) */
    return (*pa+5);   /* return by value */  
}

int main (int ac, char**av) {
    int b=7, c; 
    c = fr(&b);  /* The original value in b is changed */
    printf ("b=%d c=%d\n", b, c);  /* prints 8 and 13 */
}

Returning by reference is less obvious. Tt's used for example to return a reference received as argument, or related to it. Or a reference to a dynamically allocated object.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Can you please explain how it is in c. C doesn't have objects. – user2682811 Nov 23 '14 at 00:10
  • 5
    The C11 standard defines object as *"region of data storage in the execution environment, the contents of which can represent values"* (section 3.15). Think of it as a variable if you prefer. – Christophe Nov 23 '14 at 00:15
  • OOP languages didn't invent the term "object", which was an ordinary English word for centuries before computers existed. The most general [sense](https://en.wikipedia.org/wiki/Object_(computer_science)) is something like "chunk of memory, semantically intended to be treated as a cohesive unit, the state of which has semantic relevance to a program". – Karl Knechtel Jul 14 '23 at 15:13