user1220815
user1220815

Reputation: 37

Procedure handling Pascal

I am writing a program in pascal. But somehow getting errors whose reasons I dont understand. Can you please help.

Program main;

Procedure A(n : longint);
Procedure B(n : longint);
    Procedure E(n : longint);
        Procedure D(n : longint);
        begin
        WriteLn ('In D. No Call...');
        end;

    begin
    WriteLn ('In E ');
    D(n);
    end;

begin
WriteLn ('In B ');
WriteLn ('Calling A Do -1 ',n);
if n = 1 
 then 
A(1);


end;




begin
  WriteLn ('In A ');
  B(n);
  WriteLn ('Calling B  ',n);
  if(n<1)
  then 
  begin
  C(n);
  end;
  end;

   begin
   A(1);
   end.

I am trying to call proc A from main proc, then A calls B and so on. BUt I get complilation errors in C:

Here are the errors I get:
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(32,14) Error: Identifier not found "C"
prog.pas(32,17) Error: Illegal expression
prog.pas(37,4) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)

Upvotes: 0

Views: 205

Answers (2)

StuartLC
StuartLC

Reputation: 107357

Just to add to SteB's answer, there are several things potentially wrong here.

  • There is no Procedure named C
  • Even if you rename E to C, you will still get a compiler error, since A cannot call C because it is not in scope because of the nesting of the Procedures (it is in scope of B). See Static scoping in nested procedures
  • Over and above this, you will likely get a stack overflow exception at run time, because A(1) calls B(1) which calls A(1) ...

e.g. If renaming E to C and indenting:

Program main;

Procedure A(n : longint);
    Procedure B(n : longint);
        Procedure C(n : longint);
            Procedure D(n : longint);
                begin
                    WriteLn ('In D. No Call...');
                end;
            begin
                WriteLn ('In C ');
                D(n);
            end;
        begin
            WriteLn ('In B ');
            WriteLn ('Calling A Do -1 ',n);
            if n = 1 
                then A(1);  (* Compiles OK, B is nested within A, but watch recursion *)
        end;
    begin
        WriteLn ('In A ');
        B(n);
        WriteLn ('Calling B  ',n);
        if(n < 1)
            then 
                begin
                    C(n); (* Doesnt compile, since C is nested within B and not in scope of A *)
                end;
    end;
begin
    (* You are in Main here *)
    A(1);
end.

Upvotes: 2

SteB
SteB

Reputation: 2007

I think you've missed out "procedure C".
I'm not seeing a "procedure C" defined anywhere, the error indicates that the program also cannot see this routine.

Some better indentation could make things a lot clearer, you're using nested procedures (not really best practice), but procedures A and B have the same level of indentation.

Upvotes: 1

Related Questions