Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are cases when you need to declare the function prototype beforehand, i.e. the compiler needs to know a functions prototype before you are able to use that function.</p> <p>Consider these functions that do nothing particularly useful:</p> <pre><code>int foo(int x) { if(x &lt; 1) return x; else return x + bar(x-1); } int bar(int x) { if(x &lt; 3) return x; return x * foo(x-1); } </code></pre> <p>If you try to compile this, the compiler gets mad at you:</p> <blockquote> <p>error: 'bar' was not declared in this scope</p> </blockquote> <p>You need to put the missing prototypes in front of the function using it:</p> <pre><code>int bar(int); // as above unchanged </code></pre> <p>This is the only case where the compiler requires you to put a function prototype before your functions. In all other cases it is perfectly legal to put prototypes anywhere as often as you'd like, so this is also legal:</p> <pre><code>int foo(int); int bar(int); int foo(int); int bar(int); </code></pre> <p>Though obviously redundant (please don't do that). Some people consider it good style to put a function prototype of every function within a file at the top of the file, because</p> <ol> <li>you don't have to care about cases in which the compiler requires you to do it anymore, and some people apparently cannot interpret the error message by the compiler (which I think is very concise), and</li> <li>you see on one view what functions are provided by the file and how they are called.</li> </ol> <p>But this is exactly that, a style discussion. I like to keep my code as short as possible, so I would only use the prototypes that are required by the compiler, but that's purely a matter of taste and convention. When in Rome, do as the Romans or something like that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload