------------------------------------------------------------------------------
MC logo
Call-by-Name
[^] Functions
------------------------------------------------------------------------------
[Ch. 1: Overview and History] [Syntax] [Names and Scope] [Types and Type Systems] [Semantics] [Functions] [Memory Management] [Imperitive Programs and Functional Abstraction] [Modular and Class Abstraction] [Functional Programming] [Logic Programming]
[Call-by-Reference] [Call-by-Value-Result] [Call-by-Name] [Dyn Link Example] [Dyn Link Example] [C++ Translation of Java Dyn Link] [Variable Location] [Parameter Passing Method Exercise]
pbyn.alg
begin
    comment This is an example of pass-by-name.;
    procedure f(a, b, c);
        integer a, b, c;

        begin
            a := b;
            b := a + c;
            outstring(1, "c = ");
            outinteger(1, c);
            outstring(1, "\n")
        end;

    begin
        integer x, y;

        comment This one is just like call-by-reference, except for the value
                of c printed by the procedure.;
        x := 10;
        y := 25;
        f(x, y, 5);
        outstring(1, "x, y = ");
        outinteger(1, x); 
        outinteger(1, y); 
        outstring(1, "\n");

        comment when formulas are sent, things get interesting.  When the
                procedure changes a or b, the value of c also changes.;
        x := 10;
        y := 25;
        f(x, y, 2*x + y);
        outstring(1, "x, y = ");
        outinteger(1, x); 
        outinteger(1, y); 
        outstring(1, "\n")
    end
end