Search code examples
functioncalldeclaration

Confused about function declarations and prototypes


I have a simple question but its bugging me. My online text book says this is true --> "A function declaration enables calls to the function before the function definition." I thought this was false because if a function is called only after it was declared, how does the program know what it does. I figured it would cause a compile time error. Can someone explain why this is true? thanks.


Solution

  • It means that once you declare the function, the compiler will know about it's existence.

    So you can then write code that calls that function and the compiler will not complain. That's probably why your textbook says enables calls to the function before the function definition.

    Of course, you must then define the function somewhere (write the body of the function) for the program to execute correctly.

    The text does not mean that the declaration is enough for it to work, it only states that declaring the function allows to write code that calls it without having the definition in place yet.