Board index » delphi » Need help with recursive functions in Turbo Pascal 7.0

Need help with recursive functions in Turbo Pascal 7.0

I have encountered a problem while trying to set up a series of
recursive functions using Turbo Pascal 7.0. I know that pascal requires
that a function must be defined before it is referenced. Is there a way
to pre-define a function so it is defined before the official
declaration? My problem involves 2 recursive functions, which each make
calls that call each other and one must come before the other. Any help
on this topic would be appreciated.

 

Re:Need help with recursive functions in Turbo Pascal 7.0


Quote
Michael Rupp <mrup5...@stu.oru.edu> writes:
> I have encountered a problem while trying to set up a series of
> recursive functions using Turbo Pascal 7.0. I know that pascal
> requires that a function must be defined before it is
> referenced. Is there a way to pre-define a function so it is
> defined before the official declaration? My problem involves 2
> recursive functions, which each make calls that call each other
> and one must come before the other. Any help on this topic
> would be appreciated.

----------------------------------------------------------------
A recursive function is one that calls itself.  Two different
functions calling each other is sometimes called a circular
reference.  They are NOT the same thing.  You could probably use
the FORWARD command to get your program to compile, but as a
general rule, circular references are considered a poor
programming practice.
----------------------------------------------------------------
Derek Asari
eq...@cleveland.freenet.edu

Re:Need help with recursive functions in Turbo Pascal 7.0


You can use FORWARD parameter after procedure declaration.
But you can sent some code to ci...@EUnet.yu so I can help you more.
--
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
Regards to You all from

Igor Colovic, Belgrade, Yugoslavia, Europe

ci...@EUnet.yu

Michael Rupp <mrup5...@stu.oru.edu> wrote in article
<34F3B4C4.2...@stu.oru.edu>...

Quote
> I have encountered a problem while trying to set up a series of
> recursive functions using Turbo Pascal 7.0. I know that pascal requires
> that a function must be defined before it is referenced. Is there a way
> to pre-define a function so it is defined before the official
> declaration? My problem involves 2 recursive functions, which each make
> calls that call each other and one must come before the other. Any help
> on this topic would be appreciated.

Re:Need help with recursive functions in Turbo Pascal 7.0


Quote
Michael Rupp wrote:
> ...
> My problem involves 2 recursive functions, which each make
> calls that call each other and one must come before the other.

Hint: look for 'forward' keyword in your pascal manual or help.

--

...Si hoc tibi placet mitte nobis XX dollaria...

Re:Need help with recursive functions in Turbo Pascal 7.0


Quote
Derek T. Asari wrote:
> Michael Rupp <mrup5...@stu.oru.edu> writes:

> > I have encountered a problem while trying to set up a series of
> > recursive functions using Turbo Pascal 7.0. I know that pascal
> > requires that a function must be defined before it is
> > referenced. Is there a way to pre-define a function so it is
> > defined before the official declaration? My problem involves 2
> > recursive functions, which each make calls that call each other
> > and one must come before the other. Any help on this topic
> > would be appreciated.
> ----------------------------------------------------------------
> A recursive function is one that calls itself.  Two different
> functions calling each other is sometimes called a circular
> reference.  They are NOT the same thing.  You could probably use
> the FORWARD command to get your program to compile, but as a
> general rule, circular references are considered a poor
> programming practice.

     I believe this is called "double recursion", and there are numerous

applications where it is perfectly legal and sensible to use.  One
classical
example is a text formatter.  One procedure is a "line reader", another
one is a "line writer".  The reader reads input lines and calls the
writer,
which re-formats them, writes them, then calls the reader for more
lines.

Bob Schor
Pascal Enthusiast

Re:Need help with recursive functions in Turbo Pascal 7.0


Instead of using Forward the procedure can be declared
in the interface part of a unit, it has the same effect
as forward inside the unit.

Franz Glaser
http://members.eunet.at/meg-glaser

Re:Need help with recursive functions in Turbo Pascal 7.0


Quote
Michael Rupp wrote:

> I have encountered a problem while trying to set up a series of
> recursive functions using Turbo Pascal 7.0. I know that pascal requires
> that a function must be defined before it is referenced. Is there a way
> to pre-define a function so it is defined before the official
> declaration? My problem involves 2 recursive functions, which each make
> calls that call each other and one must come before the other. Any help
> on this topic would be appreciated.

Forward declarations.

Function Curse1 (a, b: double): double; forward;
Function Curse2 (c, d: double): double; forward;

Then down in the code section of your file, repeat the function header
(without the forward keyword), and put the code to implement it
immediately below, such as

Function Curse1 (a, b: double): double;
begin
  IF a * b = 0
    THEN Curse1 := 0
    ELSE Curse1 := a + Curse2 (b, a)
end;

Posted and Mailed.
--
Greg Shubert   Synergetics Incorporated  (On site at Hewlett-Packard)
               gshub...@fc.hp.com        (970) 898-2977

Re:Need help with recursive functions in Turbo Pascal 7.0


In article <34F442D9.D7D5E...@vms.cis.pitt.edu>,
Bob Schor  <bsc...@vms.cis.pitt.edu> wrote:

Quote
>Derek T. Asari wrote:

>> Michael Rupp <mrup5...@stu.oru.edu> writes:

>> > I have encountered a problem while trying to set up a series of
>> > recursive functions using Turbo Pascal 7.0. I know that pascal
>> > requires that a function must be defined before it is
>> > referenced. Is there a way to pre-define a function so it is
>> > defined before the official declaration? My problem involves 2
>> > recursive functions, which each make calls that call each other
>> > and one must come before the other. Any help on this topic
>> > would be appreciated.
>> ----------------------------------------------------------------
>> A recursive function is one that calls itself.  Two different
>> functions calling each other is sometimes called a circular
>> reference.  They are NOT the same thing.  You could probably use
>> the FORWARD command to get your program to compile, but as a
>> general rule, circular references are considered a poor
>> programming practice.

>     I believe this is called "double recursion", and there are numerous

>applications where it is perfectly legal and sensible to use.

I think the term you are looking for is "indirect recursion". I think double
recursion is a very bad thing where recursive procedure calls itself
twice, like the infamous recursive procedure for Fibonacci numbers.

Quote
> One
>classical
>example is a text formatter.  One procedure is a "line reader", another
>one is a "line writer".  The reader reads input lines and calls the
>writer,
>which re-formats them, writes them, then calls the reader for more
>lines.

Sounds like spaghetti to me. A good use of indirect recursion is
expression parser:

expression = term {+|- term}
term = factor {*|/ factor}
factor = number | ( expression )

Osmo

Re:Need help with recursive functions in Turbo Pascal 7.0


Quote
Derek T. Asari wrote in message

<6d0kv5$op...@pale-rider.INS.CWRU.Edu>...

Quote

>----------------------------------------------------------------
>A recursive function is one that calls itself.  Two different
>functions calling each other is sometimes called a circular
>reference.  They are NOT the same thing.

but they are recursive if the first calls the second and this calls
the first again before returning. And it's not necessarily bad
practice.

FP

Re:Need help with recursive functions in Turbo Pascal 7.0


In article <34F49FCB.6...@fc.hp.com>,
Greg Shubert ETW  <gshub...@fc.hp.com> wrote:

Quote
>Michael Rupp wrote:

>> I have encountered a problem while trying to set up a series of
>> recursive functions using Turbo Pascal 7.0. I know that pascal requires
>> that a function must be defined before it is referenced. Is there a way
>> to pre-define a function so it is defined before the official
>> declaration? My problem involves 2 recursive functions, which each make
>> calls that call each other and one must come before the other. Any help
>> on this topic would be appreciated.

>Forward declarations.

>Function Curse1 (a, b: double): double; forward;
>Function Curse2 (c, d: double): double; forward;

>Then down in the code section of your file, repeat the function header
>(without the forward keyword), and put the code to implement it
>immediately below, such as

>Function Curse1 (a, b: double): double;

You were smart to include the parameters. Standard Pascal does not
allow including the parameters and return type on the definition of the
Body. TP fortunately fixes this.

Quote
>begin
>  IF a * b = 0
>    THEN Curse1 := 0
>    ELSE Curse1 := a + Curse2 (b, a)
>end;

Osmo

Other Threads