Odd function declaration in Tower of Hanoi Solution

Posted on 2004-10-31
Last Modified: 2010-04-15

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 {
int main(void) {

  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?

Question by:Raydot
    LVL 22

    Expert Comment

    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.  

    LVL 3

    Author Comment

    "declare the types alongsides the parameters" -- huh?  

    LVL 22

    Accepted Solution

    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.

    LVL 3

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now