computer science goes bonk / all posts / rss / about

abomination and astonishment

Did you know that in C, a function protoype of the form:

void f();

...declares a function f that can accept any number of arguments of any type?

In other words, the following program is legal C:

#include <stdio.h>

void f();

int main() {
  /* yup, this 'f' is that 'f' */
  f("really?", 42, 3.999);
  return 0;
}

void f(char *str, int i, double d) {
  printf("%s %d %f\n", str, i, d);
}

To declare a function that takes no arguments, you'd use:

void f(void);

So "nothing" means "anything", and void means "nothing". Ugh.

Apparently, C didn't even have the f(void) notation until the first ANSI C in 1989. Stroustrup actually introduced this notation in the early 80's during the development of C with Classes (which later became C++).

Thankfully, Stroustrup followed an empirical approach to language design. When his users complained that his new f(void) was awkward and that C's old f() was misleading, he listened, despite reservations about introducing too many differences with C:

"It took support from both Doug McIlroy and Dennis Ritchie for me to build up the courage to make this break from C. Only after they used the word abomination about f(void) did I dare give f() the obvious meaning." D&E, p.41

So in C++, f() means exactly what you'd expect: a function f that takes no parameters.

Another win for the principle of least astonishment. Now if we could just get rid of that whole "variables default to int" thing.