user3135198
user3135198

Reputation: 43

Pascal - Using procedure before it's defined

I have a piece of code that, very simplified, looks like this

program helloworld;
a:integer;

procedure alpha;
begin
writeln('This is procedure alpha');
beta;
end;

procedure beta;
begin
writeln('This is procedure beta');
alpha;
end;

begin
readln(a);
if a=1 then alpha;
if a=2 then beta;
end.

That is of course an example, but you get what am I trying to say. I know where the error is - I'm trying to use a procedure, that has not been defined yet -, but what I'm trying to figure out is how to make pascal "ignore" it until it really becomes a problem (here it's not a problem, because procedure beta is defined later)?

TLDR; How to use procedure, that has not yet been defined?

Upvotes: 4

Views: 921

Answers (1)

Darkhogg
Darkhogg

Reputation: 14185

Pascal only knows about what has already been defined, so you need a forward declaration before any of the procedures :

procedure beta; forward;

procedure alpha;
begin
    writeln('This is procedure alpha');
    beta;
end;

procedure beta;
begin
    writeln('This is procedure beta');
    alpha;
end;

However, note that your code will create an infinite loop of calls that will generate either an stack overflow (quite appropriately) or an infinite loop, depending on your compiler.

Upvotes: 4

Related Questions