Reading history

Function Declaration vs Definition in C

6 min read 5 views

Learn the difference between declaring and defining a function in C, why prototypes matter, and how headers and multiple files use them.

A function declaration tells the compiler a function exists: its name, what it takes and what it returns. A function definition supplies the body, the actual code. You can declare a function as many times as you like, but you must define it exactly once. Getting this distinction right is what lets C programs grow across many files.

In this lesson you will learn

  • The exact difference between a declaration and a definition
  • What a prototype is and where to put it
  • How headers share declarations between files
  • The errors caused by missing or mismatched prototypes

Declaration and Definition: The Difference

int square(int x);              /* declaration: ends with a semicolon, no body */

int square(int x) {             /* definition: has a body in braces */
    return x * x;
}
DeclarationDefinition
ContainsReturn type, name, parameter typesAll of that, plus the body
Ends withA semicolonA closing brace
How many allowedAny number (if they all agree)Exactly one in the whole program
Creates code?NoYes
Usually lives inA header file (.h)A source file (.c)

A definition also counts as a declaration. If a function is defined above the place where it is called, no separate declaration is needed.

Prototypes and Where They Go

A declaration that lists the parameter types is called a prototype. The C compiler reads a file from top to bottom, so it must see a prototype before the first call. That lets you write main at the top and the helper functions below it.

#include <stdio.h>

double average(const int a[], int n);     /* declaration (prototype) */

int main(void) {
    int runs[] = {45, 12, 78, 33};
    printf("average runs: %.2f\n", average(runs, 4));
    return 0;
}

double average(const int a[], int n) {    /* definition: the body */
    int total = 0;
    for (int i = 0; i < n; i++) total += a[i];
    return (double)total / n;
}

Output

average runs: 42.00

Thanks to the prototype, the compiler knows when it reaches the call in main that average takes an array and an int and returns a double. It can check the arguments and convert them correctly.

  • Parameter names are optional in a prototype: double average(const int[], int); is valid. Including names makes it easier to read.
  • For a function with no parameters, write int menu(void);. An empty list, int menu();, meant “unspecified parameters” in C before C23 and turned off argument checking.

Splitting a Program Across Files

In a larger program, functions are defined in one .c file and called from others. A header file holds the shared prototypes, and every file that needs them includes it.

/* temp.h - declarations only */
#ifndef TEMP_H
#define TEMP_H

double c_to_f(double c);
double f_to_c(double f);

#endif
/* temp.c - the definitions */
#include "temp.h"

double c_to_f(double c) { return c * 9.0 / 5.0 + 32.0; }
double f_to_c(double f) { return (f - 32.0) * 5.0 / 9.0; }
#include <stdio.h>
#include "temp.h"

int main(void) {
    printf("36.6 C = %.1f F\n", c_to_f(36.6));
    printf("100 F  = %.1f C\n", f_to_c(100.0));
    return 0;
}
gcc -std=c11 -Wall -Wextra main.c temp.c -o convert
./convert

Output

36.6 C = 97.9 F
100 F  = 37.8 C

Here is what happens at each stage:

  1. While compiling main.c, the compiler sees only the prototypes from temp.h. That is enough to check the calls.
  2. While compiling temp.c, it builds the actual functions. Because temp.c also includes temp.h, the compiler checks that the definitions match the declarations.
  3. The linker then connects the calls in main.c to the code in temp.c.

Headers and how to write them are covered in Header Files in C. The keywords that control which names are visible across files are explained in internal and external linkage.

Missing or Mismatched Prototypes

MistakeWhat you seeFix
Calling a function before any declarationimplicit declaration of function 'average'. C99 removed implicit declarations, and GCC 14 and later reject them by defaultAdd a prototype above the call, or include the right header
Prototype and definition disagreeconflicting types for 'average'Make them identical; include the header in the defining file so the compiler checks
Declared but never definedLinker error: undefined reference to 'average'Write the definition, or add the file that contains it to the build
Defined in two filesLinker error: multiple definition of 'average'Keep one definition; never put function bodies in a header (except static inline ones)
Forgetting #include <math.h>Warnings about sqrt, or wrong results in old compilersInclude the standard header that declares the function

The last row is a classic. Without a prototype, very old C compilers assumed every function returned int, so a function returning double gave garbage. Always compile with -Wall and treat these warnings as errors.

Declarations and Definitions for Variables

The same idea applies to global variables:

extern int high_score;     /* declaration: "it exists somewhere" (goes in a header) */
int high_score = 0;        /* definition: creates the variable (in one .c file) */

Without extern, a variable line at file level is a definition, so putting it in a header that several files include leads to “multiple definition” errors.

Practice Exercises

Exercise 1. Which of these are declarations only? (a) int max(int, int); (b) int max(int a, int b) { return a > b ? a : b; } (c) extern int count; (d) int count = 5;

Show answer

(a) and (c) are declarations only. (b) and (d) are definitions.

Exercise 2. main calls print_bill(), which is defined below main. What do you add, and where?

Show answer

Add the prototype void print_bill(void); above main, or move the whole definition above main.

Exercise 3. A header contains int tax(int amount) { return amount / 10; } and is included by two .c files. What goes wrong?

Show answer

Each file gets its own definition of tax, so linking fails with “multiple definition”. Keep only int tax(int amount); in the header and move the body to one .c file.

Frequently Asked Questions

Is a prototype the same as a declaration?

A prototype is a declaration that includes the parameter types. Today almost every function declaration is a prototype.

Do I need prototypes if all my functions are above main?

No, each definition already declares its function. Prototypes become necessary when calls come before definitions or when functions live in other files.

Can I declare the same function twice?

Yes, as long as every declaration matches. That is exactly what happens when several files include the same header.

Declaration vs Definition Summary

  • A declaration announces a function; a definition provides its body.
  • Declare as often as you like; define exactly once.
  • Put prototypes in headers and definitions in .c files.
  • Include your own header in the file that defines the functions, so the compiler can check them.
  • “Implicit declaration” means a missing prototype; “undefined reference” means a missing definition.
Lessons are also marked complete when you click Next.