Board index » delphi » Using a Procedure within a Procedure?

Using a Procedure within a Procedure?

I cant seem to execute procdure MENU_CHOICE within MENU_CHOICE

Is this possible or do I need to do something else?
Code is below:

PROCEDURE MENU_CHOICE;

BEGIN
              <SNIP>

               writeln ('Please press 1 to go back to the main menu');
               c := readkey;
               CASE c OF
               '1': MENU_CHOICE;

                <SNIP>

 

Re:Using a Procedure within a Procedure?


Quote
BHD wrote in message <8ds42d$5u...@the-fly.zip.com.au>...
>I cant seem to execute procdure MENU_CHOICE within MENU_CHOICE

>Is this possible or do I need to do something else?
>Code is below:

>PROCEDURE MENU_CHOICE;

>BEGIN
>              <SNIP>

>               writeln ('Please press 1 to go back to the main menu');
>               c := readkey;
>               CASE c OF
>               '1': MENU_CHOICE;

>                <SNIP>

Pascal will let you call a procedure from within a procedure.
In fact it will let you call procedure X from within procedure X.
This is called a recursive procedure, which is exactly what
you have.

However, recursive procedures are normally used for things
like recursive directory searches. They are not meant to be
used for loops like yours. I suspect you want to do something
different: You want to return to the main menu from a submenu
whenever the user selects "1". This requires a different
structure, e.g.

Procedure MainMenu (var ReturnCode: byte);
Begin
...
end;

Procedure Submenu1 (var ReturnCode: byte);
Begin
...
end;

Procedure Submenu2 (var ReturnCode: byte);
Begin
...
end;
{ ====================== }
Begin
    repeat
        MainMenu(MenuCode);
        Case MenuCode of
            1: SubMenu1;
            2: SubMenu2;
            9:;
            0:;
        end;
    until MenuCode=0;
end.

This code will launch SubMenu1 when the user
presses 1 and SubMenu when he/she presses2.
When the user presses 9 in any menu it will
relaunch MainMenu.
When the user presses 0 in any menu the program
will terminate.

KlausL

Re:Using a Procedure within a Procedure?


Hi,

on Sat, 22 Apr 2000 at 11:56:06 o'clock, BHD wrote:

Quote
> I cant seem to execute procdure MENU_CHOICE within MENU_CHOICE

Take heart and try it, and you'll see that you can.

Quote
> Code is below:

The code given, if comleted in a trivial way, WILL work. Given the
string you print to the screen, I doubt that it is what you want.
Calling the procedure again will hardly "return" somewhere. Instead it
is called recursively.

 - Sebastian

Re:Using a Procedure within a Procedure?


No it is not possiable to run a procedure inside itself,
this could I you sink of it end in a big mess if that was
possiable.
A suggestion to your problem could be:

Repeat
 writeln('Press 1 to activate menu again ');
 writeln('Press E to exit program ');
 repeat until keypressed;
 c:=readkey;
until c <> '1';
if c='E' then exit;

* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping.  Smart is Beautiful

Re:Using a Procedure within a Procedure?


In article <8ds42d$5u...@the-fly.zip.com.au>,

Quote
BHD <ben...@zipworld.com.au> wrote:
>I cant seem to execute procdure MENU_CHOICE within MENU_CHOICE

>Is this possible or do I need to do something else?
>Code is below:

>PROCEDURE MENU_CHOICE;

>BEGIN
>              <SNIP>

>               writeln ('Please press 1 to go back to the main menu');
>               c := readkey;
>               CASE c OF
>               '1': MENU_CHOICE;

As it was said that is not a good idea. Turbo Pascal does not perform
tail recursion elimination so that would end up one step deeper in the
stack. Even if the tail recursion was eliminated it would be very poor
Pascal. If we were talking about lisp or prolog that would be another
matter. Make it a loop.

Now recursion us usable with menus, then there are submenus. That is
you pass the menu as parameter and then in case of submenu call it
recursively for the submenu. In general recursion always requires
parameter passing.

Osmo

Other Threads