+ 1
What is a function prototype?
What happens "under the hood" when we write a function prototype? Is it: 1. A pre-definition of a function (a definition where you only implement the specifier, return type and parameters without the actual function body) - when the linker finds the definition, it combines the interface with the actual function body? or 2. Solely a reference to a function (like a pointer would be a reference to a memory address)?
1 Antwort
+ 2
There are times were you must define a first because b calls it, but can't because a also calls b so it needs b defined first. The prototype lets you define a without it's code to fix this circular reference. It also lets you put definitions of both a and b in a header file so other code can call them without knowing their details.
When the compiler sees usage of a prototype, it leaves the address to call for the linker to fix. The other calls could be made relative to current code or also left for the linker. The linker maps the code to the physical memory locations followed by fixing all addresses that were unknown at compiling time. Note: changing the order that the compiler generated object files are seen by the linker changes the physical address that code gets placed in the executable, but has no effect on the execution unless there are duplicate definitions of a symbol and the linker is designed to ignore that.