Why are function declaration mandatory in C++ and not in C?

14,706

Solution 1

In a discussion that involves both C and C++ "function declaration" is a rather vague term. These languages are significantly different in this regard.

In C++ language there's only one kind of function declaration: declaration with all parameter types and return type. Such declarations are necessary because C++ language supports function overloading. In order to choose which function to call the compiler needs to know everything about the function and needs to know which overloaded versions of the function are available. If you "forget" to declare some overloaded version, it will not be considered by overload resolution. That is at least one of the reasons function declarations are necessary in C++.

In C language there are two kinds of function declarations: non-prototype declarations and prototype declarations (or simply prototypes). A prototype in C is pretty similar to C++ declaration - it includes all parameter types. Prototypes have always been required in standard C for variadic functions (functions with ... parameters). For non-variadic functions prototype declarations are not required even today. But starting from C99 at least non-prototype declarations are required for all other functions. In older C89/90 version of the language function declarations for non-variadic functions were not required.

So, that should basically answer your question. In C++ function declarations are required because language features rely on them critically. In modern C function declarations are also required just to make the code safer. In older versions of C function declarations were not required mostly simply because the language was defined to work without them.

Solution 2

Function declarations in C are not mandatory for legacy / backwards compatability reasons - if they were made mandatory then some old / legacy code somewhere would stop compiling.

I'd guess that they are mandatory in C++ becasuse C++ isn't a strict superset of C and so can make the sensible choice of making them mandatory.

You should always declare them however - see this question Must declare function prototype in C?

FYI in C99 function declarations are now mandatory.

Solution 3

Function declarations are mandatory in C. Prototypes, however, are optional, except in the cases of variadic functions and functions whose argument types would be altered by default promotions.

Share:
14,706
Achint
Author by

Achint

Updated on June 03, 2022

Comments

  • Achint
    Achint about 2 years

    So one of my previous exams had this question, and till now I've been reading that you don't need a declaration in any of the languages?

    Which is right? Will C++ give an error if there's no declaration, or will it run?

  • helpermethod
    helpermethod about 13 years
    This doesn't really give an answer to the question.
  • Lightness Races in Orbit
    Lightness Races in Orbit about 13 years
    How does it not answer the question "Will C++ give an error if there's no declaration, or will it run?"
  • helpermethod
    helpermethod about 13 years
    Because it says: "Why are function declaration mandatory in C++". I doesn't say why (btw I didn't downvote you).
  • Puppy
    Puppy about 13 years
    @Tomalak: Why was it designed that way? Your answer doesn't answer the question.
  • Lightness Races in Orbit
    Lightness Races in Orbit about 13 years
    @DeadMG: You're referring to the question title. I responded to the actual question content. Besides, "why did the C++ standards committee design things such and such a way" is highly subjective and not a good question on Stack Overflow.
  • Achint
    Achint about 13 years
    @Tomalak: Actually I had a compilers and programming language course, and there is an apparent logic and reasoning behind how each of the languages was designed. The point of my question was, what differentiating factor between C++ and C make it mandatory for C++ to have a declaration while allowing C to not necessarily have. I don't think the answer would have been subjective.
  • Lightness Races in Orbit
    Lightness Races in Orbit about 13 years
    @Achint: Only the person who made the decision can answer that precisely. For anyone else, it's a guessing game.