34,333
edits
Changes
→Function Prototypes
== Function Prototypes ==
Where a function is declared in relation to where it is called from is a significant factor. In the same way that most cultures reads read a page from top to bottom, a compiler reads an Objective-C source file from top to bottom. If the compiler comes across a call to a function before it has found the function declaration it has to make assumptions about the type of result that function returns. The default assumption is that the function will return an ''int''. Having made this assumption, if when the compiler finally reaches the function declaration, an error will have to reported if it is found to return a data type other than an ''int''. To see this in action, try compiling the following code:
<pre>
</pre>
The compiler is complaining because it had assumed when the function was called at line 10 that it returned an ''int'' because up until that point it has had not found a function declaration to tell it otherwise. Having made this assumption it then found the function declaration and discovered it actually returned a ''float'', thereby causing a conflict.
There are two solutions to this problem. One is to always declare function before they are called:
Note that the semi-colon (;) is mandatory.
We can now applying apply this concept to our earlier example:
<pre>