The standard Pascal (ISO 7185) doesn't have a
'return' statement, so a function or procedure will
execute to the end. I often encounter situations in
which multiple sequential checks must be done so
that:
a. the failure of one check interrupts the whole
sequence and
b. each seccessive check may use the result cal-
culated in the previous check.
This can be represented like many nested IF state-
ments, which is ugly when the nesting level is high.
Languages with a non-structured 'return' statement
allow to linearize this, but what to do in Pascal?
I thought about looping through an array of fucntion
pointers, but this seems too much of an overhead to
me...
Thank you in advance,
Anton
If you limit yourself to std pascal, afaik your options are:
1. GOTO to somewhere near the end of the procedure
2. GOTO directly to the parent procedure. (if nested procedure)
the second is mainly interesting if you want to execute special
errorhandling in the parent procedure.
Chuck Mowle, a former colleague of mine, once suggested an approach that
goes something like this:
(* untested! *)
checkNo := 0;
WHILE ok AND (checkNo < maxChecks) DO BEGIN
INC(checkNo);
CASE checkNo OF
1:
2: .....
...
...
END (* case *)
END (* while *)
IF NOT ok THEN Write("failed on check", checkNo);
Regards,
Chris Burrows
CFB Software
Astrobe v3.3: ARM Oberon-07 Development System
http://www.astrobe.com
Anton
You don't actually have to nest the IF statements, you can have a
variable that indicates whether or not a check has failed and then
test this variable before each step in the sequence.
So for example instead of
if check1 then
begin
do something1
if check2 then
begin
do something2
if check3 then
begin
do something3
end
end
end
end
you could use
ok := check1;
if ok then
begin
do something1;
ok := check2;
end;
if ok then
begin
do something2;
ok := check3;
end;
if ok then
begin
do something3;
end;
I write code like this sometimes in C (which does have a return
statement) when I don't want to use the return because I have a
function which needs to do some cleanup before it exits.
> [...]
> if ok then
> begin
> do something2;
> ok := check3;
> end;
>
> if ok then
> begin
> do something3;
> end;
> [...]
Thank, this is another good idea.
Anton