Mutually recursive functions in C

Andrew Koenig ark at alice.UUCP
Mon Feb 13 01:20:46 AEST 1989


In article <34823 at tut.cis.ohio-state.edu>, 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?

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 at europa.att.com



More information about the Comp.lang.c mailing list