Mutually recursive functions in C

Peter Rowell peter at thirdi.UUCP
Mon Feb 13 06:24:56 AEST 1989


william at 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 at thirdi.UUCP



More information about the Comp.lang.c mailing list