MC logo

Parameter Passing and Scope Problem


CS 404: Solutions and Examples

Consider the following Pascal program:
PROGRAM Fred(input, output);
    VAR
        moe, larry, curly: integer;

    PROCEDURE Wumpus(ana, susan, sara: integer);
        BEGIN
            ana := susan + 4;
            larry := sara;
            susan := susan - 1;
            writeln(curly)
        END;

    PROCEDURE Bumpus;
        VAR
            larry, lucretia, i: integer;
            bill: array[1..20] OF integer;
        BEGIN
            FOR i := 1 TO 20 DO bill[i] := i + 1;

            larry := 77;
            lucretia := 44;
            Wumpus(lucretia, curly, lucretia);
            writeln(larry);

            larry := 5;
            Wumpus(larry, bill[larry], larry + 3);

            FOR i := 1 TO 20 DO write(bill[i], ' ');
            writeln;
        END;

    BEGIN
        moe := 10;
        larry := 20;
        curly := 30;
        Bumpus;
        writeln(moe, ' ', larry, ' ', curly)
    END.
Answer the following:
  1. What is the output of the program?

  2. What is the output if we change the header of Wumpus to PROCEDURE Wumpus(var ana, susan: integer; sara: integer);?

  3. What is the output if we assume (contrary to Pascal rules) that the parameters are passed by name?

  4. What is the output of the program if we assume (contrary to Pascal rules) that scope dynamic rather than static? (Assume no other changes from Pascal.)

>>