1

Is there any proper example for explaining call-by-result ? (not pseudocode)

I have learned that ALGOL 68, Ada could use this way,
but I cannot find any clear example of Call-by-Result.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62
구마왕
  • 488
  • 6
  • 20
  • 1
    As far as I can see in Ada, the term "call-by-result" is almost never used, it's much easier to think of parameter passing modes - i.e. say what you want to happen rather than how you want it to happen. Specifically, look for any code that uses `OUT` parameters, (Not `IN OUT` which would correspond to call-by-value-result). –  May 13 '16 at 13:13
  • What does "call-by-result" mean? Is it a parameter passing method? – Jacob Sparre Andersen May 13 '16 at 14:51
  • [This answer](http://stackoverflow.com/a/3004067/40851) explains `in out`, which as @BrianDrummond says corresponds to call-by-value-result. You can probably deduce call-by-result (i.e. the Ada `out`) from it. – Simon Wright Jun 02 '16 at 08:02

1 Answers1

0

I just made by myself.

pseudocode

begin
integer n;
procedure p(k: integer);
    begin
    n := n+1;
    k := k+4;
    print(n);
    end;
n := 0;
p(n);
print(n);
end;

Implement using Ada Language

call.adb

with Gnat.Io; use Gnat.Io;

procedure call is
x : Integer;
Procedure  NonSense (A: in out integer) is  
begin
    x := x + 1;
    A := A + 4;
    Put(x);
end NonSense;

begin 
    x := 0;
    NonSense (x);
    Put(" ");
    Put(x);
    New_Line;
end call;

Since Ada uses call-by-result way, result should be 1 4. (It could be checked by entering this code into online-Ada-compiler "http://www.tutorialspoint.com/compile_ada_online.php")

And, other result applied different passing parameter types should be...
call by value: 1 1
call by reference: 5 5
(compare > call by value-result: 1 4)

구마왕
  • 488
  • 6
  • 20
  • 2
    Remember that parameter passing methods in Ada depends on the type. Integer types are passed by copy, while (e.g.) tagged types are passed by reference. For some types the choice is up to the compiler implementer. – Jacob Sparre Andersen May 13 '16 at 14:52
  • 2
    Ideally the Ada compiler would refuse to compile this code, on the grounds that it’s a bug waiting to happen. – Simon Wright May 13 '16 at 16:54
  • 2
    A possible hint is in the LRM, 6.2: " If an object is of a type for which the parameter passing mechanism is not specified and is not an explicitly aliased parameter, then it is a bounded error to assign to the object via one access path, and then read the value of the object via a distinct access path". If you do need pointers, use pointers. It is best, however, to forget about passing mechanisms in favor of IN, IN OUT, and OUT, when writing Ada. – B98 May 13 '16 at 16:59