In C, if a function is called before its declaration, the compiler assumes return type of the function as int.
For example, the following program fails in compilation.
The following program compiles and run fine because return type of fun() is changed to int.
What about parameters? compiler assumes nothing about parameters. Therefore, the compiler will not be able to perform compile-time checking of argument types and arity when the function is applied to some arguments. This can cause problems. For example, the following program compiled fine in GCC and produced garbage value as output.
There is this misconception that the compiler assumes input parameters also int. Had compiler assumed input parameters int, the above program would have failed in compilation.
It is always recommended to declare a function before its use so that we don’t see any surprises when the program is run (See this for more details).
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- _Noreturn function specifier in C
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- A C/C++ Function Call Puzzle
- Variables and Keywords in C
- Functions in C/C++
- Implicit return type int in C
- Write a C program that won't compile in C++
- Name Mangling and extern "C" in C++
- Importance of function prototype in C
- Const Qualifier in C
- How to Count Variable Numbers of Arguments in C?
- exit(), abort() and assert()
- Static functions in C
- Why C treats array parameters as pointers?
- What is the purpose of a function prototype?