Board index » delphi » Calling main function

Calling main function

Is there a way to call your main function from a procedure? I want to
user to have the option of restarting the program

thanks
halem...@cadvision.com

 

Re:Calling main function


Quote
Brennan Hale-Matthews wrote:
> Is there a way to call your main function from a procedure?

Are you a C programmer?

Quote
> I want to user to have the option of restarting the program

Declare a label at the very beginning of the main program and jump to it
whenever you want to restart the program. Note that this will not execute
the initialization routines of the included routines again, though.

label restart;

begin
  restart:
  ...
  ...

  { Restart the program }
  goto restart
end.

--
This message will self-destruct in 10 seconds

Re:Calling main function


Quote
Brennan Hale-Matthews wrote:

> Is there a way to call your main function from a procedure? I want to
> user to have the option of restarting the program

This is extremely suspective! :-)

Of course you could do it with a label, as Frederic suggested. But who
will re-initilize the variables and do the initialisation stuff in
the units?

???
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser/ http://members.xoom.com/f_glaser/

Re:Calling main function


I tried... What i want to do is this little bit of code
procedure yesno;
var answer : char;
begin
    writeln('Try again? y/n?');
     readln(answer);
     if answer = 'y' then begin
    main; {or whatever... however you would call the main function}
    end
    else begin
    end;
end;

Once it calls the main procedure/function it would basically go through the
whole program once again.
Is there a way you can call a label in your main function from a procedure?

Quote
Frederic wrote:
> Brennan Hale-Matthews wrote:

> > Is there a way to call your main function from a procedure?

> Are you a C programmer?

> > I want to user to have the option of restarting the program

> Declare a label at the very beginning of the main program and jump to it
> whenever you want to restart the program. Note that this will not execute
> the initialization routines of the included routines again, though.

> label restart;

> begin
>   restart:
>   ...
>   ...

>   { Restart the program }
>   goto restart
> end.

> --
> This message will self-destruct in 10 seconds

Re:Calling main function


Quote
Archie1 wrote:

> I tried... What i want to do is this little bit of code
> procedure yesno;
> var answer : char;
> begin
>     writeln('Try again? y/n?');
>      readln(answer);
>      if answer = 'y' then begin
>     main; {or whatever... however you would call the main function}
>     end
>     else begin
>     end;
> end;

> Once it calls the main procedure/function it would basically go through the
> whole program once again.
> Is there a way you can call a label in your main function from a procedure?

STOP!

This is not an appropriate approach for a Pascal programmer!

Write the procedure, which you treat as the "main" procedure as a
real / true procedure. And then write a while or repeat loop in the
main program.

It really seems that you are thinking in "C" vocabulary, using
"function" instead of "procedure".

Begin
  Repeat
    MainProcedure; {what you think to be the main() }
    Write('Once again');
    Readln(answer);
  Until answer <> 'Y';
End.      

:-)
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser/ http://members.xoom.com/f_glaser/

Re:Calling main function


On Sun, 08 Aug 1999 11:58:56 -0700, Brennan Hale-Matthews

Quote
<halem...@cadvision.com> wrote:
> Is there a way to call your main function from a procedure? I want to
> user to have the option of restarting the program

If you really want to have the option to restart the program from
scratch the only safe solution is the one every good ol' mailbox
program uses. Start the program from a batch file:

        rem test.bat
        :restart
        myprog.exe
        if errorlevel 43 goto end
        if errorlevel 42 goto restart
        :end

        program myprog;
        var
          s:string;
        begin
          writeln('Do you want to restart ? (yes)');
          readln(s);
          if s='yes' then halt(42)
        end.

Regards
Horst

Re:Calling main function


In article <37ADD370.2F2B5...@cadvision.com>, Brennan Hale-Matthews
<halem...@cadvision.com> writes

Quote
>Is there a way to call your main function from a procedure? I want to
>user to have the option of restarting the program

You don't. What you do is to RETURN NATURALLY to the main program (which
should contain a loop if you REALLY want to go round again).

--
Marcus Morris - South Croydon, LONDON, UK (Mar...@ntos.demon.co.uk)

Re:Calling main function


Quote
Archie1 wrote:
> I tried... What i want to do is this little bit of code
> procedure yesno;
> var answer : char;
> begin
>     writeln('Try again? y/n?');
>      readln(answer);
>      if answer = 'y' then begin
>     main; {or whatever... however you would call the main function}
>     end

OK, put the entire thing in a WHILE...DO or REPEAT...UNTIL loop, so that your
main procedure looks something like this:

BEGIN
    REPEAT
        MainFlowOfControlEtc;
        Write('Try again ([Y]/n) ? ');
        ans:=ReadKey;
    UNTIL Upcase(ans)='N';
END.

Of course you'll need to put USES CRT at the top, since ReadKey is used (in my
example anyway).  Upcase() is useful in cases like this too, as demonstrated.

Quote
>     else begin
>     end;
> end;

And you also had the above code fragment attached to your original post.  It
seems to be superfluous...

Re:Calling main function


JRS:  In article <37AF1BCA.553EB...@hippo.ru.ac.za> of Mon, 9 Aug 1999
20:19:55 in news:comp.lang.pascal.borland, Yusuf Motara

Quote
<s...@hippo.ru.ac.za> wrote:
>BEGIN
>    REPEAT
>        MainFlowOfControlEtc;
>        Write('Try again ([Y]/n) ? ');
>        ans:=ReadKey;
>    UNTIL Upcase(ans)='N';
>END.

>Of course you'll need to put USES CRT at the top, since ReadKey is used (in my
>example anyway).  Upcase() is useful in cases like this too, as demonstrated.

One can avoid needing Crt, in this instance, with

function ReadKey : char ; assembler ;
asm mov AH,0 ; int 16H end {ReadKey} ;

N.B. that is not an exact replacement for all of Crt.ReadKey; test it
out.  In fact, subfunction $10 may be more useful.

--
 ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
  <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c. FAQqish topics, links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Re:Calling main function


Quote
>One can avoid needing Crt, in this instance, with

>function ReadKey : char ; assembler ;
>asm mov AH,0 ; int 16H end {ReadKey} ;

>N.B. that is not an exact replacement for all of Crt.ReadKey; test it
>out.  In fact, subfunction $10 may be more useful.

Why don't you want to use Crt? The linker only adds the
really used procedures to your program. Maybe ...
I don't have the source for Crt, maybe there's a "big"
initialization part in it?

--
  [ http://home.t-online.de/~Matthias.Buechse/ ] - [ Matthias.Buec...@T-Online.de ]
  [ Homepage German only - vote for an English version; use my E-Mail above       ]

Re:Calling main function


In article <37AF1BCA.553EB...@hippo.ru.ac.za>,
Yusuf Motara  <s...@hippo.ru.ac.za> wrote:
Quote

>OK, put the entire thing in a WHILE...DO or REPEAT...UNTIL loop, so that your
>main procedure looks something like this:

>BEGIN
>    REPEAT
>        MainFlowOfControlEtc;
>        Write('Try again ([Y]/n) ? ');
>        ans:=ReadKey;
>    UNTIL Upcase(ans)='N';
>END.

One should add:

if ans=#0 then readkey;

after the ans:=readkey

Otherwise it would also react to some special keys.

Osmo

Re:Calling main function


Quote
Osmo Ronkanen wrote:
> In article <37AF1BCA.553EB...@hippo.ru.ac.za>,
> Yusuf Motara  <s...@hippo.ru.ac.za> wrote:

> >OK, put the entire thing in a WHILE...DO or REPEAT...UNTIL loop, so that your
> >main procedure looks something like this:

> >BEGIN
> >    REPEAT
> >        MainFlowOfControlEtc;
> >        Write('Try again ([Y]/n) ? ');
> >        ans:=ReadKey;
> >    UNTIL Upcase(ans)='N';
> >END.
> One should add:

> if ans=#0 then readkey;

> after the ans:=readkey

> Otherwise it would also react to some special keys.

> Osmo

Yes, but the options are given as "([Y]/n)", meaning that "Y" is the default
option if you choose to press an arrow key or whatever.  Besides, if a person were
to press a special key, the key would not be "Y" anyway, so it's not necessary =)

-=Yusuf=- cunningly deprives the user of many options

PS Of course to catch some silly {*word*81} pressing multiple keys before this choice
even displays, a loop like

WHILE KeyPressed DO
    ReadKey;

would be a primitive solution.  That would stop inadvertent choosing of the "Y"
option....

Re:Calling main function


JRS:  In article <7oor2o$q2...@news04.btx.dtag.de> of Tue, 10 Aug 1999
10:08:53 in news:comp.lang.pascal.borland, Matthias Bchse

Quote
<Matthias.Buec...@T-Online.de> wrote:
>>One can avoid needing Crt, in this instance, with

>>function ReadKey : char ; assembler ;
>>asm mov AH,0 ; int 16H end {ReadKey} ;

>>N.B. that is not an exact replacement for all of Crt.ReadKey; test it
>>out.  In fact, subfunction $10 may be more useful.

>Why don't you want to use Crt? The linker only adds the
>really used procedures to your program. Maybe ...
>I don't have the source for Crt, maybe there's a "big"
>initialization part in it?

You have not been here long, I think?

Borland's Crt unit is monolithic; if any is used, all is included.
Borland's V7 Crt gives RunTime error 200 in initialisation on a PC over
about 200MHz.

Not much can go wrong with code as short as I offer above.  I believe
(untested) that it will return "n" for F37 = Alt-F7, which is the only
harmful case for the context in question; those who are concerned have
but to change "char" to "word" and alter the test accordingly.

--
 ? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
  <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c. FAQqish topics, links.
  Timo's TurboPascal <A HREF="ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip">FAQ</A>.
  <A HREF="http://www.merlyn.demon.co.uk/clpb-faq.txt">Mini-FAQ</A> of c.l.p.b.

Re:Calling main function


Quote
> Why don't you want to use Crt? The linker only adds the
> really used procedures to your program. Maybe ...
> I don't have the source for Crt, maybe there's a "big"
> initialization part in it?

   You're _partially_ right here, but wrong insofar as the CRT Unit is
concerned.  Yes, Smart Linking (the TP/BP feature which binds/links on
only referenced data and subprograms from Units) works quite nicely.  
However, it doesn't work at all for the CRT Unit, since it's written in
assembler, which can't make use of Smart Linking.
   Thus, S/L will do a lot of good for most user-produced Units, but has
no meaning to CRT (and perhaps the graphics ones, as well, I don't know
the details)...

Re:Calling main function


Quote
Yusuf Motara wrote:

[..]

Quote
> Yes, but the options are given as "([Y]/n)", meaning that "Y" is the default
> option if you choose to press an arrow key or whatever.  Besides, if a person were
> to press a special key, the key would not be "Y" anyway, so it's not necessary =)

> -=Yusuf=- cunningly deprives the user of many options

> PS Of course to catch some silly {*word*81} pressing multiple keys before this choice
> even displays, a loop like

> WHILE KeyPressed DO
>     ReadKey;

> would be a primitive solution.  That would stop inadvertent choosing of the "Y"
> option....

If you don't check for special keys, you _always_ should do so! Because
otherwise pressing e.g. an arrow key would cause the same effect as
pressing 'Y' _two_ times!

regards
--
Arno Fehm (af...@bigfoot.de)

------------------------------------------------------------------------
Member of Grey Dreams Design: visit http://GreyDreams.home.pages.de !!!!
He who can destroy a thing has the real control over it. (Frank Herbert)
------------------------------------------------------------------------

Go to page: [1] [2]

Other Threads