Hacker News new | ask | show | jobs
by pjmlp 2515 days ago
Here is pass by reference and pass by pointer in Pascal.

    type

    Point = record
        x, y : integer
    end;

    procedure ByPointer(p : ^Point);
    begin
        p^.x := 12
    end;

    procedure ByReference(var p : Point);
    begin
        p.x := 12;
        p.y := 33
    end;

    procedure example
    var
      p : Point
    begin
       p.x := 23;
       p.y := 28;
       WriteLn('The point is ', p.x, ' ', p.y);

       ByPointer(@p)
       WriteLn('The point is ', p.x, ' ', p.y);

       p.x := 23;
       p.y := 28;
       WriteLn('The point is ', p.x, ' ', p.y);

       ByReference(p)
       WriteLn('The point is ', p.x, ' ', p.y);
    end;

Actually the concept is already exposed in Algol, a couple of years before C was even an idea.
1 comments

good point, so, here, it is exactly syntax sugar as, unlike in C++, nothing else except syntax is different from the pointer-based version. Not to mention that, in the article, I was mentioning the general concept that has one (and single) variant, in C and Java, and 2 in Pascal or C++