2012-11-13 66 views
0

我正在用帕斯卡寫程序。但不知何故得到我不明白原因的錯誤。你能幫忙嗎?程序處理帕斯卡爾

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. 

我想從主proc調用proc A,然後A調用B等等。 BUt我在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) 

回答

1

我想你錯過了「過程C」。
我沒有看到任何地方定義的「過程C」,錯誤表明程序也看不到這個例程。

一些更好的縮進可以使事情更清晰,你使用嵌套過程(不是最好的做法),但過程A和B具有相同的縮進級別。

2

只需添加到SteB的答案,這裏有幾件事情可能是錯誤的。

  • 沒有名爲C
  • 即使你重新命名EC過程,你仍然會得到一個編譯錯誤,因爲A不能稱之爲C,因爲它不在範圍內,因爲程序的嵌套(它在範圍B)。見Static scoping in nested procedures
  • 除了以上提到的這一點,你可能會在運行時得到一個堆棧溢出異常,因爲A(1)電話B(1)這就要求A(1) ...

例如如果將E更名爲C並縮進:

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.