• C

# Odd function declaration in Tower of Hanoi Solution

Hi,

I was recently looking for a recursive solution to the "Tower of Hanoi" problem, and I came across this:

void move(n, A, C, B)
int n,A,B,C;
{
if (n==1)
printf("Move from %d to %d.\n",A,C);
else {
move(n-1,A,B,C);
move(1,A,C,B);
move(n-1,B,C,A);
}
}

int main(void) {
move(4,1,3,2);

return 0;
}

I understand the solution, what I don't understand is the function declaration!  Why are n, A, B, and C declared just outside of the function prototype, and where is the function prototype?

Raydot.
LVL 3
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
This is an old-style function declaration.  It took the C Wizards about a decade to figure out it made more sense to declare the types alongsides the parameters.

0
Author Commented:
"declare the types alongsides the parameters" -- huh?

0
Commented:
The original C syntax was:

<function type> <function name> ( <param name1>, <param name2>, .... )   <param 1 type >  name1,  <param 2 type>  name2, ...

in other words:   void move(n, A, C, B)    int n,A,B,C;

----------------------------------------
this was mighty silly and repetetive syntax, just like the old FORTRAN way of declaring parameters.

Somewhere along the line the parameter syntax was loosened up so you
could use the newer syntax:   void move( int n, int a, int c, int b );
This also made it possible to pre-declare the function and parameter types.
------------------------------------------------------

I would change the functions to use the new-style headers.  The semantics should be just the same.

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
What the problem was is that the program didn't work when I converted it to the "new" function declaration, but that turned out to be because I re-wrote the prototype as

void move(int n, int A, int B, int C)

and not

void move(int n, int A, int, C, int B)

...and hence my confusion.  Thanks for your help.
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.