Call by Value and Call by Reference in C

// Expert user has replied.
S Subrata Sasmal 1 year 10 months ago
16 3 0

I'm trying to understand the conceptual difference between call by reference, value, and name.

So I have the following pseudocode:
<code>foo(a, b, c)
{
b =b++;
a = a++;
c = a + b*10
}

X=1;
Y=2;
Z=3;
foo(X, Y+2, Z);</code>
What's X, Y, and Z after the foo call if a, b, and c are all call by reference? if a, b, and c are call-by-value/result? if a, b, and c are call-by-name?

Another scenario:
<code>X=1;
Y=2;
Z=3;
foo(X, Y+2, X);</code>
I'm trying to get a head start on studying for an upcoming final and this seemed like a good review problem to go over. Pass-by-name is definitely the most foreign to me.

Reference - https://www.scaler.com/topics/c/call-by-value-and-call-by-reference-in-…

Please register or login to post a reply

3 Replies

S Sean Kennedy

This article may be more relevant to your understanding, since this shows some demonstrable outputs based on the input conditions.

https://www.javatpoint.com/call-by-value-and-call-by-reference-in-c

S Sean Kennedy

Just to also ring ones noodles - with RUST programming language - this is left a little Vague.
( the older documentation has the FAQ - the newer site here: does not...)
https://prev.rust-lang.org/en-US/faq.html#why-no-stable-abi

Really most Rustaceans learn rust via this portal: https://doc.rust-lang.org/stable/rust-by-example/

v vineet jadav

I will break down this pseudocode based on different parameter passing mechanisms by taking this example: 

foo(X, Y+2, Z);

If parameters are passed by reference:

  • X is passed by reference, so its value will change to whatever a becomes.
  • Y+2 is an expression, not a variable, so it cannot be passed by reference.
  • Z is passed by reference.

After the function call, X will be updated to 11 (since a is 1 and a++ is 2, then c = 1 + 2*10 = 21 and X will be assigned 21), Y and Z remain unchanged.

If parameters are passed by value/result:

  • X, Y+2, and Z are all passed by value/result.
  • The values of X, Y, and Z remain unchanged because the function operates on copies of the values.

If parameters are passed by name:

  • The function is essentially pasted into the code wherever it's called, with its parameters substituted directly.
  • X will be updated to 11 (as per the call-by-reference scenario).
  • Y+2 and Z will remain unchanged because they are not modified within the function.

Check out Cloud Computing and DevOps courses to learn and implement these codes.

CONTACT
Can’t find what you’re looking for?