0

Suppose that I have defined a function that takes in arguments as follow:

double aspectRatio (double length, double width) {
    double ratio = length/width;
    return ratio;
}

When I prototype the function before the main function, is it ok to leave out the parameter list, and leave the prototype as the following:

double aspectRatio();

Or is it a must to include the parameter list in the prototyping stage as well?

The reason that I asked this is because the program seems to run fine even without me including the parameters in the function prototype.

Also, are there any disadvantage of not including the parameters in the function prototype?

I also understood from the other posts that in C, an empty parameter () is different from a void parameter (void). That is, the following functions behave differently:

void printHelloWorld(void) {
    printf("Hello World");
}

void printHelloWorld() {
    printf("Hello World");
}

Would like to know what are the differences as well. Thanks!

xhxh96
  • 649
  • 1
  • 7
  • 14
  • 1
    It is very bad practice. It was allowed in C90 because it was necessary (all existing code would have been broken were it not allowed). Functions without a full prototype in scope should not be used in code written this millennium. – Jonathan Leffler Sep 04 '17 at 16:54
  • `double aspectRatio();` tells the compiler, not to assume anything abouth the parameters, that is, you can pass anything to the function, which is of course a bad thing. – Bite Bytes Sep 04 '17 at 16:55
  • If you leave out the parameter list then you still have a declaration of the function, but it is not a *prototype*. This does not interfere with a prototype being provided as well, but if no prototype is in scope at the point of call then it has implications for argument conversions. – John Bollinger Sep 04 '17 at 17:13

0 Answers0