Thuta Learning
IntermediateProgrammingbeginner

Function Declaration

Relax. We'll talk through this in plain words — no textbook voice.

The C compiler reads code from top to bottom. main() — if you write a function's definition below it, you need to declare the function's declaration/prototype above first.

c
#include <stdio.h>

// Function declaration
int add(int a, int b);

int main() {
  int result = add(5, 3);
  printf("Result: %d", result);
  return 0;
}

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

int add(int a, int b); tells the compiler ahead of time, "a function like this will show up later." The definition is where you write what the function actually does.

You should see
Result: 8

Info

Keep the return type, parameter types, and order matching between the declaration and the definition.

Easy traps

  • A prototype needs a semicolon, but a function definition doesn't end with one — the definition needs a body { } instead.