Just wrote this for a friend who’s in the process of writing C… it’s been a while for me, but I think it’s mostly accurate:
So you have your function main().
main() calls a function called int foo(int x).
say you have a file that looks like this:
when the file is compiled, main has no idea what foo() is, if you’re even calling it right, etc. because it doesn’t know about foo() until after main is compiled. There are two ways you can fix this:
- Define foo before main, like this:
This works when you have functions that only need to be used in one file. But it won’t work if that function is called from functions in many different files.
2. Describe how foo() expects to be called before it is ever called, so main() can tell if it’s calling it right:
This lets you be more flexible, because the declaration int foo(int x); can exist in a bunch of different files that all need to call foo(), but you only need to repeat the first line and not the entire function. This way, anyone calling foo() doesn’t even need to care about what foo() does, just what they expect to shove into it and what they expect to get out of it.
Hope that helps.