The problem with using the Procedure in Pascal

119 Views Asked by At
uses GraphABC;
var 
  x, y, a: integer;

  procedure treug(x, y, a, b: integer);
  begin
    x := 0;
    y := 20;
    line(x, y, x+a, y);
    line(x, y, x, y+b);
    line(x+a, y, x, y+b);
  end;

begin
  writeln('Enter the length of the catheter'); 
  read(a);

  while y < 480 do
  begin
    y := y+1;
    treug(x, y, a, a);
  end;
end. 

Outputs only one triangle, and not the desired number up to the vertical border.

I expected one vertical row of right triangles (x and y are the coordinates of the right angle) to the lower border of the graphic window.

1

There are 1 best solutions below

1
Dúthomhas On
uses GraphABC;
var 
  x, y, a: integer;

  procedure treug(x, y, a, b: integer);
  begin
    line(x, y, x+a, y);
    line(x, y, x, y+b);
    line(x+a, y, x, y+b);
  end;

begin
  writeln('Enter the length of the catheter'); 
  read(a);

  x := 0;   // <-- initialize your x and y here
  y := 20;

  while y < 480 do
  begin
    y := y+1;   // (see note below)
    treug(x, y, a, a);
  end;
end. 

Remember, you have two sets x and y variables:

  • The global ones declared in the main program (and now properly initialized in the main program)
  • The ones local to the treug() procedure.

Things may have the same name and not be the same thing. If it helps, you can think of the global x as GraphABC.x and the local x as GraphABC.treug.x.

BTW, you probably want to increment y by something more than 1 each time through the loop, otherwise it’ll look just like a single triangle has been smeared down the display. (You might want to increment by b.)