Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Pascal Ansi -iso > Re: Pascal Proc...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 120 of 211
Post > Topic >>

Re: Pascal Procedures Ordering

by Hanford Carr <"hwcarr<no spam>"@[EMAIL PROTECTED] > Dec 13, 2004 at 07:22 PM

pocm@[EMAIL PROTECTED]
 wrote:
> 
> Hi all,
> 
> I'm not a Pascal programmer but I'm trying something with it. I'm
> defining 2 functions and I'm trying to call them with mutual recursion
> as follows:
> program mrecur (i, o);
> 
> function doodd(i :  integer) : integer;
> begin
> doodd := i + doeven(i - 1);
> end; { doodd }
> 
> function doeven(i :  integer) : integer;
> begin
> if (i = 0) then 0
> else
> doeven := i + doodd(i / 2);
> end; { doeven }
> 
> var init, final : integer;
> begin
> read(init);
> if even(init) then final := doeven(init)
> else
> final := doodd(init);
> end.
> 
> However, I cannot have doeven before doodd or the other way around
> since each one calls the other. How can I solve this? In C I would
> write the function prototypes before their declaration but can't seem
> to solve this in Pascal. Can someone give me a hint?
> Cheers,
> 
> Paulo Matos

Try a forward declaration.

function doeven(i :  integer) : integer;  forward;

> function doodd(i :  integer) : integer;
> begin
> ...
> end; { doodd }
> 
> function doeven(i :  integer) : integer;
> begin
  ...
> end; { doeven }
> 

Cheers Hanford




 1 Posts in Topic:
Re: Pascal Procedures Ordering
Hanford Carr <"hw  2004-12-13 19:22:14 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 0:32:20 CDT 2008.