3

I understand the basic logic of call by copy restore. But I was wondering for a problem like this

void p(int x,int y) {
     x++;
     y+=2;
}

main() {
   int a=10;
   p(a,a);
   // what will be now value of a, 11 or 12?
}
alienCoder
  • 1,461
  • 3
  • 17
  • 23
  • Have you tried running this? Is this supposed to be C or something else? In C the value of `a` will be 10 (assuming `=++` was intended to be `++`) but I'm not sure I'm clear on what you're asking. Particularly, what is `x=++` intended to be? `x++`? – asm Dec 20 '13 at 15:19
  • What language is this? It seems the code doesn't compile either. "x=++" ? – Chris Leyva Dec 20 '13 at 15:19
  • I am not talking about any language here, I am just asking about call by copy restore method. – alienCoder Dec 20 '13 at 15:57
  • Neither. After that call, you will still have `a==10`. `x` and `y` within `p()` are *copies* of `a`, and changes to them are not back-propagated... – twalberg Dec 20 '13 at 16:37
  • I am pretty sure in copy-restore method it will be either 11 or 12 but the confusion is which value will be the final one. – alienCoder Dec 20 '13 at 16:47
  • 3
    Perhaps you'll need to explain this "copy-restore method", and where you heard about it. It's not something that is in the C or C++ languages, which have call-by-value and call-by-reference. Perhaps this is a different language you are talking about (although the posted code appears to at least be an approximation of C)? – twalberg Dec 20 '13 at 16:51
  • @alienCoder When you are talking about copy-restore, you **are** talking about a language, because most languages don't support copy-restore. Posting a code sample in your question that looks like C (a language that does not support copy-restore) is a bit misleading. A few more details would have made this a really great question! – Johannes Fahrenkrug Dec 20 '13 at 18:26
  • 1
    @alienCoder I think you got your answer down there. – Chris Leyva Dec 20 '13 at 22:04

1 Answers1

4

Update: The answer is 12, see update below.

OK, this is actually a good question. So this explains what "copy-restore" is all about: https://stackoverflow.com/a/8871340/171933

Most programming languages don't support copy/restore, but only (some variations) of pass-by-value and pass-by-reference. So it's not so easy to try this out.

However, the question you are interested in is this: Which value wins? Does x get to write its value back to a when the function ends (which would be 11), or does y get to write its value back to a when the function ends (which would be 12).

In a language that supports "copy-restore", I'd hope that this would throw a compiler error.

Update:

After some searching I've found a language that actually supports "copy-restore", namely Ada. This is the code in Ada (this is my first and probably last program written in Ada):

with Ada.Text_IO; use Ada.Text_IO;

procedure copy_restore_example is
        a: integer;

        procedure p(x: in out integer; y: in out integer) is
        begin
                x:= x+1;
                y:= y+2;
        end p;

begin
        a := 10;
        Put_Line("Before :" & natural'image(a));
        p(a, a);
        Put_Line("After :" & natural'image(a));

end copy_restore_example;

The result is 12, y wins. You can run this program in your browser here: http://www.compileonline.com/compile_ada_online.php

Community
  • 1
  • 1
Johannes Fahrenkrug
  • 42,912
  • 19
  • 126
  • 165