← The C Language Lesson 30 of 77 · 10 XP

Declaring functions

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

The compiler reads the file from top to bottom. If main calls a function that's defined further down, the compiler doesn't know about it yet. The solution is a declaration (a prototype): the function's header with ; instead of a body:

int square(int x);        // a declaration: "such a function exists"

int main(void)
{
    printf("%d\n", square(4));
    return 0;
}

int square(int x)         // the definition — somewhere below or in another file
{
    return x * x;
}

Header files consist of exactly such declarations: stdio.h is the prototypes of printf, scanf and other functions, while their code lives in a library.

Big programs are split like this:

  • math_utils.h — the declarations;
  • math_utils.c — the definitions;
  • main.c — #include "math_utils.h" (in quotes, because it's your own file).

Declarations let functions call each other in any order — for example, in mutual recursion.

Task. The functions is_even and is_odd are defined after main and call each other. Add declarations at the top of the file so that the program compiles, and print even or odd for the non-negative number read.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.