If there is no function prototype, a function is implicitly declared by its first appearance in an expression.
In C, if a function return anything else than int
it is a good habit to declare the function inside the caller function like 1st example of code.
But you are always constrained by the compiler to write the prototype, the reason is that he does not know who the function is because it is declared underneath the main()
function.
The question is: These two are equivalent? Will either writing the prototype or declaring explicitly the function inside the main()
return the wanted result? How can it return a bad value if you are constrained always to use one of this two ways?
- if the function is declared in the same script as the caller function is (here
main()
)- prototype
- declare explicitly function in
main()
.
- if the function is declared in another file
- prototype
- declare explicitly function in
main()
E.g:
int main()
{
double doSomething(int a);
printf("%d", doSomething(2) );
}
double doSomething(int a)
{
return a * 2.0;
}
vs
double doSomething(int a);
int main()
{
printf("%d", doSomething(2) );
}
double doSomething(int a)
{
return a * 2.0;
}
This thread is almost what i needed but it did not answer all my questions. C language - calling functions without function prototype