Board index » delphi » Calling a procedure within a procedure

Calling a procedure within a procedure

I've got several procedures in my program and they are all calling other
procedures. However i've found that i can only call another procedure if that
procedure is physically located above that procedure which causes big problems
as its also calling another procedure.

E.g.
Procedure1
Procedure2- call procedure 4
Procedure 3 -call procedure1, call procedure 2
Procedure4-call procedure 5
Procedure5-END.

 

Re:Calling a procedure within a procedure


Quote
In article <20010320032837.05389.00001...@ng-mo1.news.cs.com>, Emmarich1 wrote:
>I've got several procedures in my program and they are all calling other
>procedures. However i've found that i can only call another procedure if that
>procedure is physically located above that procedure which causes big problems
>as its also calling another procedure.

>E.g.
>Procedure1

procedure 4 [exactly same parameter definition as below]; FORWARD;

Quote
>Procedure2- call procedure 4
>Procedure 3 -call procedure1, call procedure 2

procedure 5 [exactly same parameter definition as below]; FORWARD;
Quote
>Procedure4-call procedure 5
>Procedure5-END.

Re:Calling a procedure within a procedure


Quote
Emmarich1 wrote:

> I've got several procedures in my program and they are all calling other
> procedures. However i've found that i can only call another procedure if that
> procedure is physically located above that procedure which causes big problems
> as its also calling another procedure.

> E.g.
> Procedure1
> Procedure2- call procedure 4
> Procedure 3 -call procedure1, call procedure 2
> Procedure4-call procedure 5
> Procedure5-END.

Why not sort them as follows?
procedure1
procedure5
procedure4
procedure2
procedure3

Mind you, I'm aware of the fact (as Marco van de Voort suggested)
using FORWARD may be a simpler way out (and, indeed, the only way
out, when the calls between procedures form cycles, e.g. proc1
calling proc2, proc2 calling proc3 and proc3 calling proc1).

Has it actually happened to somebody that using FORWARD was absolutely
necessary? Any such calls must obviously be conditional, for otherwise
you get an endless loop.

--
Jyrki Lahtonen, docent
Department of Mathematics,
University of Turku,
FIN-20014 Turku, Finland

http://users.utu.fi/lahtonen
tel: (02) 333 6014

Re:Calling a procedure within a procedure


Quote
In article <3AB7285C.28DF3...@utu.fi>, Jyrki Lahtonen wrote:
>Emmarich1 wrote:

>procedure4
>procedure2
>procedure3

>Mind you, I'm aware of the fact (as Marco van de Voort suggested)
>using FORWARD may be a simpler way out (and, indeed, the only way
>out, when the calls between procedures form cycles, e.g. proc1
>calling proc2, proc2 calling proc3 and proc3 calling proc1).

>Has it actually happened to somebody that using FORWARD was absolutely
>necessary? Any such calls must obviously be conditional, for otherwise
>you get an endless loop.

Yes, very often. Parsers often need forward defined procedures.

Forward can also be handy for scope reasons in some isolated cases, since it
allows you to define the procedure implementation further down.

Re:Calling a procedure within a procedure


Quote
> Has it actually happened to somebody that using FORWARD was absolutely
> necessary? Any such calls must obviously be conditional, for otherwise
> you get an endless loop.

Yes, had to do with recursive programming which did not fit in one
procedure.

But I also use FORWARD for an other reason nowadays. I try to give
functions/procedures a name which tell me what they do like "AddToFile". But
if this proc uses the function "ReadFromD64", I have to place it above the
first one. With a few procedures and functions that's no problem. But with a
lot of them I use FORWARD to put them in alphabetical order. Until now I
have no troubles with it like loose of speed etc.

If someone thinks it has disadvantages, please let me know.

Groetjes, Ruud

http://Ruud.C64.org/

Re:Calling a procedure within a procedure


Quote
>procedure 4 [exactly same parameter definition as below]; FORWARD;

What do you mean by parameter definition? I'm not sure what the parameter is!

Re:Calling a procedure within a procedure


Quote
In article <20010320101927.20647.00000...@ng-cu1.news.cs.com>, Emmarich1 wrote:
>>procedure 4 [exactly same parameter definition as below]; FORWARD;

>What do you mean by parameter definition? I'm not sure what the parameter is!

Example: (this is NOT possible, the compiler will complain)

procedure xxx(s:string;y:integer); forward;

[..]

procedure xxx(s:string;y:longint);

Re:Calling a procedure within a procedure


In article <slrn9betd7.2l6v.mar...@toad.stack.nl>,
Marco van de Voort <mar...@toad.stack.nl> wrote:

Quote
>In article <20010320101927.20647.00000...@ng-cu1.news.cs.com>, Emmarich1 wrote:
>>>procedure 4 [exactly same parameter definition as below]; FORWARD;

>>What do you mean by parameter definition? I'm not sure what the parameter is!

>Example: (this is NOT possible, the compiler will complain)

>procedure xxx(s:string;y:integer); forward;

>[..]

>procedure xxx(s:string;y:longint);

One can also omit the parameters from the actual definition. In
Standard Pascal one has to omit them.

Osmo

Re:Calling a procedure within a procedure


Quote
Emmarich1 wrote in message

<20010320032837.05389.00001...@ng-mo1.news.cs.com>...
However i've found that i can only call another procedure if that

Quote
>procedure is physically located above that procedure which causes big
problems
>as its also calling another procedure.

in pascal, everyting has to be defined before you can use it. when looking
at the source code, everything above has been defined, and everything below
has yet to be defined (as a general rule!)

it is good that way, it helps us think structure, rather than a complete
mess

Re:Calling a procedure within a procedure


OK - thanks! it works now!

Re:Calling a procedure within a procedure


JRS:  In article <3AB7285C.28DF3...@utu.fi>, seen in
news:comp.lang.pascal.borland, Jyrki Lahtonen <lahto...@utu.fi> wrote at
Tue, 20 Mar 2001 11:52:28 :-

Quote

>Has it actually happened to somebody that using FORWARD was absolutely
>necessary? Any such calls must obviously be conditional, for otherwise
>you get an endless loop.

As a professional mathematician, you might not find my longcalc.pas (via
sig line 3) of interest.  But it does need FORWARD (which, naturally, I
only use when inevitable) :

function RPN(const St : string ; var SP : PLET) : boolean ; forward ;

RPN evaluates the string St, which contains operands to be pushed and
operators to use them (does arithmetic, bases 2..16, up to >65000
digits).  It calls subroutines to do most of its work, and these call
others, including subroutines for operators.

Simple operators like "add" are implemented in pure Pascal, but more
complicated ones like "#ge" (date of Gregorian Easter for given year),
or "#pi" (pi by the BBP method), or conversion between Y M D h m s and
(UNIX) time_t, are themselves implemented as strings to be evaluated by
RPN.

Thus there must be at least one FORWARD in the single-file program.

In a Unit, the Interface section provides declaration before use.

Web authors may be more interested in checklinx.pas, same place; it
reads local DOS copies of Web pages to find local links and follows them
to check any anchors and reads the pages to ..., except that it
remembers what it has done and does not repeat.  That too uses FORWARD,
in recursion.

In demime.pas, I seem to use FORWARD to declare a procedure "FAIL" early
so that other procedures may call it without having access to a global
variable used in FAIL; but this may be a bad (but working) program.

And countdwn.pas, a Vorderman emulator (UK TV), has procedures P, Q, R
calling each other; hence P is FORWARD.

And envicalc.pas recurses like longcalc (but is a smaller program,
usable but unfinished).

IMHO, FORWARD is, according to circumstances, either undesirable or
indispensable.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
 PAS, EXE in <URL: http://www.merlyn.demon.co.uk/programs/> - see 00index.txt.
 Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)

Re:Calling a procedure within a procedure


Quote
> Has it actually happened to somebody that using FORWARD was absolutely
> necessary? Any such calls must obviously be conditional, for otherwise
> you get an endless loop.

Several people have now convinced me that e.g. clean parsing routines
require
this. Thanks to Dr Stockton for the link - so that's what professional
code looks like. I'm impressed. As I always write code for a single
purpose,
I never had to learn all those directives and conditional defines. I
think
I could learn to use them simply by studying your code, though.

--
Jyrki Lahtonen, docent
Department of Mathematics,
University of Turku,
FIN-20014 Turku, Finland

http://users.utu.fi/lahtonen
tel: (02) 333 6014

Re:Calling a procedure within a procedure


Quote
In article <9981c8$t7...@oravannahka.helsinki.fi>, Osmo Ronkanen wrote:
>In article <slrn9betd7.2l6v.mar...@toad.stack.nl>,
>Marco van de Voort <mar...@toad.stack.nl> wrote:
>>In article <20010320101927.20647.00000...@ng-cu1.news.cs.com>, Emmarich1 wrote:
>>>>procedure 4 [exactly same parameter definition as below]; FORWARD;

>>>What do you mean by parameter definition? I'm not sure what the parameter is!

>>Example: (this is NOT possible, the compiler will complain)

>>procedure xxx(s:string;y:integer); forward;

>>[..]

>>procedure xxx(s:string;y:longint);

>One can also omit the parameters from the actual definition. In
>Standard Pascal one has to omit them.

A lot of new compilers don't support this, because it clashes with
overloading.

Other Threads