[comp.lang.c] Mutually recursive functions in C

william@cis.ohio-state.edu (James H. Williamson) (02/12/89)

	Does C allow mutually recursive functions & if so
	what are the declaration mechanisms & could somebody
	give an example?

Jim Williamson

bradb@ai.toronto.edu (Brad Brown) (02/12/89)

In article <34823@tut.cis.ohio-state.edu> william@cis.ohio-state.edu (James H. Williamson) writes:
>
>	Does C allow mutually recursive functions & if so
>	what are the declaration mechanisms & could somebody
>	give an example?
>
>Jim Williamson


Sure, C will let you do anything...  :-)

Try the following, with appropriate adjustments for your argument types:

>-------------------------------------

:
:

int func1( int argument )
{
	/*  Remember to have a forward reference for the second function  */
	/*  You might have to modify the syntax of this -- my compiler    */
	/*  accepts it, but a minimal declaration is just "int func2()"   */
	/*  with no extern -- it works but does not give you protection   */
	/*  against type mismatches in the argument to func2().           */
	extern int func2( int );

	/*  Do stuff  */
	:
	:
	func2( stuff );
	:
	:
}

int func2( int argument )
{
	/*  Don't need a forward reference for func1 here  */

	/*  Do stuff  */
	:
	:
	func1( stuff );
	:
	:
}

>-------------------------------------

That's it!

					(-:  Brad Brown  :-)
					bradb@ai.toronto.edu

ark@alice.UUCP (Andrew Koenig) (02/12/89)

In article <34823@tut.cis.ohio-state.edu>, william@cis.ohio-state.edu (James H. Williamson) writes:

> 	Does C allow mutually recursive functions & if so
> 	what are the declaration mechanisms & could somebody
> 	give an example?

Here's a factorial function needlessly written as a pair of
mutually recursive functions:

In C Classic:

	int fact();	/* not really necessary */
	int tcaf();

	int fact(n)
		int n;
	{
		if (n <= 0)
			return 1;
		else
			return n * tcaf (n-1);
	}

	int tcaf(n)
		int n;
	{
		if (n <= 0)
			return 1;
		else
			return n * fact (n-1);
	}

In ANSI C:

	int fact(int);	/* not really necessary */
	int tcaf(int);

	int fact(int n)
	{
		if (n <= 0)
			return 1;
		else
			return n * tcaf (n-1);
	}

	int tcaf(int n)
	{
		if (n <= 0)
			return 1;
		else
			return n * fact (n-1);
	}

-- 
				--Andrew Koenig
				  ark@europa.att.com

peter@thirdi.UUCP (Peter Rowell) (02/13/89)

william@cis.ohio-state.edu (James H. Williamson) writes:
>	Does C allow mutually recursive functions & if so
>	what are the declaration mechanisms & could somebody
>	give an example?

I think the best question for C is:

    "What does C *not* allow?"

The answer is "not much".  This is a shorter answer than for the
opposite question.

In this particular case, the answer is yes, C "allows" mutually
recursive functions because C really doesn't give a damn about how who
calls what from where with what.  If the routines return anything other
than "int" (or if you are using ANSI C with prototypes), you will need
to place an extern declaration in the front of the file so that the
compiler doesn't bitch about "Redeclaration of procedure Foo", but
other than that - anything goes!


How about a pair of routines that further abuse the often abused
computation of a factorial? (And, No - I didn't bother trying to run/debug
this gibberish!) (And, Yes - I know they are the same routine, but how
often do you get to write garbage like this and actually get to pretend
you're making sense?!)

extern int fact_odd();

int fact_even(x) int x;
{
    if (x <= 1)
	return(1);
    return(x * ((x & 1) ? fact_odd(x-1) : fact_even(x-1)));
}

int fact_odd(x) int x;
{
    if (x <= 1)
	return(1);
    return(x * ((x & 1) ? fact_odd(x-1) : fact_even(x-1)));
}


Someone on the net a couple of years ago made the following apt comparison:

    Pascal is like the round nose scissors you had in kindergarten -
	you can do a number of things, but you have to really *try*
	to cut yourself.

    C is like a pack of two-edged razor blades - capable of precisely
	doing a large number of things ... and if you are not very careful
	you will end up bleeding to death on the machine room floor.

    Hmmmmm..... I wonder, is that why hackers like C? :-)

----------------------------------------------------------------------
Peter Rowell				"C - the Sweeny Todd of languages."
Third Eye Software, Inc.		(415) 321-0967
Menlo Park, CA  94025			peter@thirdi.UUCP

karl@haddock.ima.isc.com (Karl Heuer) (02/14/89)

In article <447@thirdi.UUCP> peter@thirdi.UUCP (Peter Rowell) writes:
>william@cis.ohio-state.edu (James H. Williamson) writes:
>>Does C allow mutually recursive functions...
>
>[Yes.]  If the routines return anything other than "int" (or if you are using
>ANSI C with prototypes), you will need to place an extern declaration in the
>front of the file so that the compiler doesn't bitch about "Redeclaration of
>procedure Foo"...

Or, if the routines are intended to be local, then you should forward-declare
them using `static' (and hope your compiler isn't one of the broken ones that
can't handle this):
	static ftype_t f();
	static gtype_t g();
	...
	static ftype_t f() { ... g(); ... }
	static gtype_t g() { ... f(); ... }

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint