Solved

Posted on 2004-10-31

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.

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.

4 Comments

<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.

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.

By clicking you are agreeing to Experts Exchange's Terms of Use.

Title | # Comments | Views | Activity |
---|---|---|---|

C hashtable library | 3 | 50 | |

C, Typedef struct, how to define const sub-variable within union/struct, is there a way to do this? | 12 | 82 | |

UPD maximums on Red Hat | 6 | 87 | |

Problem to scan all sheets | 3 | 50 |

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

Connect with top rated Experts

**12** Experts available now in Live!