Call by result Example

7.1k views Asked by At

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.

1

There are 1 answers

3
구마왕 On

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)