Board index » delphi » forward command - heres the sample of what I ment

forward command - heres the sample of what I ment

Hello ok I guesse I should be more clear on what I ment.. here is the pas
file that I made I know it works so if you decide to use it it will work
for you..

everything I need to know is in the following

------------------------------- THIS SPOT IF FOR THE PAS FILE
--------------------------

{Cut the text that is after this message!
This does work I tested it before I posted it..
this is done on TP7.0! please send me your results to
ar...@nemaine.com}

uses
Crt;

Procedure       A; far ;
begin
ClrScr;
WriteLn('Ok you went to A, now how do I make it go to main? If main is
under this procedure?');
Readln;
ClrScr;
{main;     I need it to goto procedure main!}
end;

{----------------------------------------------------------------}
Procedure       B; far ;
begin
ClrScr;
WriteLn('Ok you went to B, now how do I make it go to main? If main is
under this procedure?');
Readln;
ClrScr;
{main;     I need it to goto procedure main!}
end;

{----------------------------------------------------------------}
Procedure       C; far ;
begin
ClrScr;
WriteLn('Ok you went to C, now how do I make it go to main? If main is
under this procedure?');
Readln;
ClrScr;
{main;     I need it to goto procedure main!}
end;

{----------------------------------------------------------------}
Procedure       main; far;
type
  CallSomething = procedure ;
var
  CallArray : array [1..3] of CallSomething ;
  CallVar : CallSomething ;
var
  SomethingNo : integer ;
begin
  CallArray[1] := A ;
  CallArray[2] := B ;
  CallArray[3] := C ;
ClrScr;
WriteLn('[1] A');
WriteLn('[2] B');
WriteLn('[3] C');
WriteLn;
WriteLn;
WriteLn('What I need is, when it it goes to procedure a b or c, it will go
back to main.');
WriteLn('but it says unknown identfier becuase main is under procedure a b
and c.. so');
WriteLn('how can I get it to go from a b or c  to main?');
WriteLn;
WriteLn;
WriteLn('Thank You,');
WriteLn('Arron Mitchell');
WriteLn;
write( 'Selection: ' ) ;
readln( SomethingNo ) ;
CallArray[SomethingNo];
end;
begin
main;
end.

------------------------------------------- END OF FILE
-----------------------------------

Please send your results to

ar...@nemaine.com

Thank You Alot,
Arron Mitchell

 

Re:forward command - heres the sample of what I ment


Quote
> Hello ok I guesse I should be more clear on what I ment..

...                                                  ^^^^ meant???
Quote
> here is the pas
> file that I made I know it works so if you decide to use it it will work
> for you..
> uses Crt;

  procedure Main; forward;
Quote
> Procedure  A; far ;

...              ^^^ why?  Why do you use Far on all your subprograms?  
If this is a program or any sort of conventional Unit, there's no need to
use the Far clause.
Quote
> begin
> ClrScr;
> WriteLn('Ok you went to A, now how do I make it go to main? If main is
> under this procedure?');
> Readln; ClrScr;
> main;  
> end;
> Procedure  B;
> begin
> ClrScr;
> WriteLn('Ok you went to B, now how do I make it go to main? If main is
> under this procedure?');
> Readln; ClrScr;
> main;    
> end;
> Procedure  C;
> begin
> ClrScr;
> WriteLn('Ok you went to C, now how do I make it go to main? If main is
> under this procedure?');
> Readln; ClrScr;
> main;  
> end;

procedure Main;
begin
  ...whatever
end;
begin
  A; B; C
end.

Re:forward command - heres the sample of what I ment


Quote
Arron Mitchell wrote:

> Hello ok I guesse I should be more clear on what I ment.. here is the
> pas
> file that I made I know it works so if you decide to use it it will
> work
> for you..

> everything I need to know is in the following

I didn't try the code, but what I was missing were the forward
statements you were supposed to be using.
Declare the procedures you want to be found throughout your program at
the beginning of the program, with forward and you'll have no problem.
I don't see what you're getting at (probably my mistake) but I thought I
sent you an example?
Oh, well...

Remco de Korte
Soft Machine / R.S.I.
(still, you could play a nive game instead: download SNAKES at:
http://www.xs4all.nl/~remcodek/download.html#snakes)

Re:forward command - heres the sample of what I ment


Quote
"Arron Mitchell" <ar...@nemaine.com> writes:
> Hello ok I guesse I should be more clear on what I ment.. here
> is the pas file that I made I know it works so if you decide to
> use it it will work for you..

==================================================================
There have been a dozen replies to the original post, most of
them dealing with "FORWARD".  Of course, "FORWARD" is a way for a
compiler to deal with references to a procedure whose address
hasn't yet been determined by the compiler.  While at first
glance this seems to address the problem, the real trouble is
that the author appears to lack some basic programming knowledge.
The example posted is an attempt to use a "GOTO" form of flow
control.

Procedures are essentially subroutines . . . execution
automatically returns to the calling module upon completion of
the procedure.  Its possible to write a program like the code
posted and have that program compile, but you eventually end up
with a stack overflow runtime error since the procedures all call
each other and never terminate.
==================================================================
Derek Asari
eq...@cleveland.freenet.edu

Re:forward command - heres the sample of what I ment


[posted and mailed as requested]

Quote
"Arron Mitchell" <ar...@nemaine.com> wrote:
>Hello ok I guesse I should be more clear on what I ment.. here is the pas
>file that I made I know it works so if you decide to use it it will work
>for you..

Indent your code a little.  It'll make it easier to read.

If you put "PROCEDURE Main; FORWARD;" at the top of your program
(e.g. before Procedure A), then main will be forward declared
and visible to all following procedures.

However, I don't think this is what you want.  Take a pencil and
trace the flow of your program.  Tracing the program flow in a
program design outline is called bench testing.  A little design
and bench testing can save lots of heart ache.  

Okay, let's trace.  The program body calls Main.  Main asks for
a number, then calls A, B, or C.  You now want the last
instruction in A, B, or C to call Main.  So again Main prompts
for a number an calls A, B, or C.  As you want it, the last
instruction would again call Main.  And so goes the round until
eventually you run out of stack space and the program abends!

When a procedure or function ends, it returns to the caller and
program execution resumes with the caller's next instruction.
It doesn't matter which function or procedure Main calls, nor
which procedure they call.  Simply let each one end to have
execution resume with the caller's next statement.

BTW, you should have Main test the input to insure that it is
within the proper range.  Of course if you develop with range
checking on {$R+} then out of range values will be caught and
terminate the program gracefully.   Care to speculate what will
happen if the error is not caught?

From the nature of your problem I can see you are just starting.
Don't get discouraged, Arron.  Things do get easier as you
learn.

    ...red

--
Support the anti-Spam amendment
  Join at http://www.cauce.org/

Other Threads