functioncallpascalprocedures

PASCAL - Call a procedure from another function


I have this problem. Im at the End of this Function:

FUNCTION ToString(Liste : Pokemon) : String;
VAR
RES : STRING;
BEGIN

  ClrScr;
  TextBackground(Green);
  Writeln('DER POKEDEX:');
  Writeln;
  WHILE (Liste <> NIL) DO
     BEGIN
      RES :=  RES + Concat('#',IntToStr(Liste^.PkmnPos), ': ', Liste^.PkmnName, '. // ', IntToStr(Liste^.PkmnKG), ' kg', chr(13),chr(10),chr(13),chr(10));
      Liste := Liste^.Next;
    END;
    TextBackground(Black);
    ToString := Res;

END;

Now, I have the Procedure called "Submenu". So, when in the Main Program code, i can just call the procedure "Submenu()". But when im within this above functions, it wont compile my code. It says "identifier not found". But, after im done with this function the last thing it needs to do is go into submenu. And im really trying not to builed an infinite loop in wihtin the main program only to not have the programm end. What is the best way of doing that?

O, and I know, that if I have the function Submenu started before the other functions, it would work. But both functions call each other, so neither can be on top of each other because there will always be one, that wont work...

Regards.


Solution

  • Then you need a forward declaration:

      FUNCTION ToString(Liste : Pokemon) : String; FORWARD;
    
      FUNCTION Submenu(); 
      BEGIN
          ..
           ToString(Liste);
          ..
      END;
    
      FUNCTION ToString(Liste : Pokemon) : String; 
      BEGIN
         // real implementation tostring
         ..
         Submenu();
         ..
      END;
    

    Note the declaration with FORWARD