Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Functions and Scope

Function declarations and definitions in C

Reference for C function declarations: prototypes versus definitions, empty parameter lists, void, implicit declarations removed in C99 and C23 changes.

A function declaration tells the compiler a function's name, its return type and the types of its parameters, so calls can be checked. A function definition also supplies the body. A declaration that is not a definition is called a prototype when it lists the parameter types, and it normally lives in a header file.

You can declare a function many times, but define it only once in the whole program. Calling a function before it is declared is an error in modern C: implicit declarations were removed in C99 and compilers now reject them.

C
int add(int a, int b);          /* declaration (prototype) */

int add(int a, int b) {         /* definition */
    return a + b;
}

Try it

C Prototype first, definition later

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C void, redeclaration and static functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
return-type name ( parameter-type-list );                   /* declaration */
return-type name ( parameter-type-list ) { statements }     /* definition */
Part Description
return-type Any object type except an array type or a function type, or void. It cannot be an array, but may be a struct or a pointer.
name The identifier. Functions have file scope and, by default, external linkage.
parameter-type-list Comma-separated types, with optional names in a declaration (required in a definition before C23 if the parameter is used). (void) means no parameters. ... as the last item makes the function variadic.
storage class static gives internal linkage; extern (the default for functions) is allowed; inline is a hint (see inline functions).

Return value / errors: a call that does not match the visible prototype in argument count or in incompatible types is a compile error. Defining two functions with the same external name is a link error ("multiple definition"). Calling a function with no declaration is a compile error since C99 (many compilers only warn in C89 mode).

Usage notes

  • Put prototypes in headers. Include the header in the file that defines the function too, so the compiler checks that definition and declaration agree. See header files.
  • () versus (void). Before C23, int f(); declared a function with unspecified parameters (old-style, no checking), while int f(void); declared none. C23 makes () mean the same as (void). Always write (void) for portability.
  • Parameter names in a prototype are only documentation. Use meaningful ones in headers.
  • Arguments are converted to the parameter types of a visible prototype (average(3, 4) passes doubles). Without a prototype, only the default promotions apply, which is one reason old code was fragile.
  • Return type must be given. Implicit int (a definition such as add(a, b) with no return type) was removed in C99 and is rejected or warned about by compilers.
  • Definition order. Define before use, or declare before use. Mutually recursive functions need at least one forward declaration.
  • static functions are private to a translation unit: no name clashes and the compiler can inline or remove them more freely.
  • Compatible redeclarations. Declarations must agree on the return type and parameter types (top-level const on a by-value parameter is ignored in the comparison).
  • main is special: see main().
  • K&R style definitions (int f(a, b) int a; int b; { ... }) were removed in C23. Do not write them.
  • [[noreturn]], [[deprecated]] and other attributes can appear on declarations; see attributes.