0

Code:

#include <stdio.h>
int navin();   //function declaration
int main()
{
    int i = navin(); //function call
}
int navin() //function definition
{
     printf("Hello");   
}

Output: Hello

Question:

If we remove the function declaration line int navin(); from the above code, it prints the same. Then why it is used?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Navin Samuel
  • 115
  • 1
  • 8
  • Without it, your compiler is assuming `navin` has no parameters and returns `int`. If the function signature was different, then there would be problems. – crashmstr Dec 09 '14 at 18:16
  • it is used as a holder, it tells the program that there is a function named navin(); this helps in C, but i am guessing you are using a compiler that can compile c++ and since it is partially object oriented it won't show you an error. – bakriawad Dec 09 '14 at 18:17
  • @skrrgwasme don't confuse a declaration with a definition. – n. m. could be an AI Dec 09 '14 at 18:24
  • regarding this line: int navin(); 1) it is a prototype, not a function declaration. 2) the current prototype indicates an unknown number of parameters. It should be int navin( void ); 3) if you have all warnings enabled, the compiler will raise a warning about using default/ implied function signature. – user3629249 Dec 09 '14 at 18:47

2 Answers2

1

The function declaration here is called the forward declaration. This is used if the function has been called [used] before it is defined.

In your code, see, when navin() is called inside main(), at that point, the function is not yet defined. By using the forward declaration, you're telling compiler,

"This is the function signature. This function definition is present later in the code, continue the compilation."

Without this forward declaration, most of the c compilers will show you a warning [termed as implicit declaration] that they are not able to find the function navin(), so, the compiler will assume the function prototype as

  • any number of input arguments
  • return type as integer.

In most of the cases, this ends up in a disaster.

You can find more information in this related question.

Profound usage: The header files.


NOTE:

In older c standards, c89, this was supposed to be only an warning, but the newer versions, c99 and c11 defines this [missing forward declaration] to be an error. However, by default gcc [c complier on linux] doesn't implement these standards. If you tell gcc explicitly to follow c99 and higher, it'll produce the correct error message.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

The function prototype is shown at the top of the file. This allows the compiler to check the code more thoroughly.

My experience is that if you did not declare the function at the top of your program, you also could define this function before you use it (before main function). for example in your program:

#include <stdio.h> int navin() //function definition { printf("Hello");
} int main() { int i = navin(); //function call }