Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

declaration

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?

like image 662
Achint Avatar asked May 03 '11 23:05

Achint


People also ask

Why do we need to declare functions in C?

Forcing the programmer to declare functions before using them allows the compiler to work in one pass (i.e. read the code only once).

What happens if we don't declare function in C?

In C, if a function is called before its declaration, the compiler assumes the return type of the function as int. For example, the following program fails in the compilation.

Can we call a function without declaration in C?

You can't call a function without declaring it first.

What is the difference between declaration and definition of a function in C?

The main difference between Function Declaration and Function Definition in C Programming is that Function declaration indicates what the function is and Function Definition indicates what the function does.


3 Answers

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.

like image 122
AnT Avatar answered Oct 13 '22 05:10

AnT


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.

like image 21
Justin Avatar answered Oct 13 '22 05:10

Justin


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.

like image 1
R.. GitHub STOP HELPING ICE Avatar answered Oct 13 '22 04:10

R.. GitHub STOP HELPING ICE