5

I've got this question, and I'm a bit confused as to what would be printed, especially for pass-by-reference. What value would be passed to x if there are two parameters? Thanks!

Consider the following program. For each of the following parameter-passing methods, what is printed?

a. Passed by value

b. Passed by reference

c. Passed by value-result

void main()
{
    int x = 5;
    foo (x,x);
    print (x);      
}

void foo (int a, int b)
{
    a = 2 * b + 1;
    b = a - 1;      
    a = 3 * a - b;
}
Berdan Akyürek
  • 212
  • 1
  • 14
Strongbuns
  • 83
  • 1
  • 1
  • 3
  • What would be your guess(es)? – Ryan J Jul 23 '14 at 00:33
  • Have you tried running it? – Avery Jul 23 '14 at 00:34
  • This is a question on a test, so I haven't tried running it. If I had to guess, I'd say a would print 5, b would print 23, and c would print 23. B is what's giving me trouble, because would x be set to 23 or would it stay at 5 if passed by reference? – Strongbuns Jul 23 '14 at 00:39
  • What the heck is "passed by value-result"? I consider myself _very_ knowledgeable about C++, but any interpretation of this I've thought of so far is unlikely. – Mooing Duck Jul 23 '14 at 00:41
  • So would it print 5, 5, and 23? or 5, 5, and 5? – Strongbuns Jul 23 '14 at 00:43
  • Pass by value-result is copy-in, copy-out essentially. It's not supported by C++, but can be simulated. See this post: http://stackoverflow.com/questions/5768721/pass-by-value-result – Ryan J Jul 23 '14 at 00:48
  • This isn't necessarily C++, this is just random code. Value would be like Java, reference would be like C++, and I'm honestly not sure what uses value-result. – Strongbuns Jul 23 '14 at 00:55

1 Answers1

6

The first two should be pretty straightforward, the last one is probably throwing you because it's not really a C++ supported construct. It's something that had been seen in Fortran and Ada some time ago. See this post for more info

As for your results, I think this is what you would get:

1)

5

2)

x = 5,
a = 2 * 5 + 1 = 11
b = 11 - 1 = 10
a = 3 * 10 - 10 = 20;  // remember, a and b are the same reference!
x = 20

3) Consider this (in C++ style). We will copy x into a variable, pass that by reference, and then copy the result back to x:

void main()
{
    int x = 5;
    int copy = x;
    foo (copy,copy);  // copy is passed by reference here, for sake of argument
    x = copy;
    print (x);      
}

Since nothing in the foo function is doing anything with x directly, your result will be the same as in #2.

Now, if we had something like this for foo

void foo (int a, int b)
{
    a = 2 * b + 1;
    x = a - 1;      // we'll assume x is globally accessible
    a = 3 * a - b;
}

Then # 2 would produce the same result, but #3 would come out like so:

a = 2 * 5 + 1 = 11
x = 11 - 1 = 10  // this no longer has any effect on the final result
a = 3 * 11 - 11 = 22
x = 22
Community
  • 1
  • 1
Ryan J
  • 8,275
  • 3
  • 25
  • 28
  • Wouldn't `a` and `b` be _separate_ copies in pass by value-result? Actually, both ways just sound like arguments _against_ this method of passing... (Basically: How independent are passed 'references' to the same object within the same function call and in what order are they 'copied back'?) – Noein Apr 25 '17 at 14:11