LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

What are function prototypes and why are they important?

A prototype is a function's signature declared ahead of its body (int f(int);), letting the compiler type-check calls and resolve forward/mutual references before it sees the definition.

// Prototype (declaration) - goes in header files
int calculate(int x, int y);
void print_result(const char *msg);

// Definition (implementation) - goes in .c files
int calculate(int x, int y) {
    return x + y;
}

Why prototypes matter:

  1. Enable mutual recursion:
// Prototype
void foo(int n);
void bar(int n);

void foo(int n) { if (n > 0) bar(n-1); }
void bar(int n) { if (n > 0) foo(n-1); }
  1. Type checking:
// Without prototype, compiler might not catch:
// Would compile silently!
int result = calculate(3.14, "oops");

// With prototype, compiler checks argument types
  1. Separate compilation:
// math_utils.h
int add(int a, int b);

// math_utils.c
#include "math_utils.h"
int add(int a, int b) { return a + b; }

// main.c
#include "math_utils.h"
int main() { return add(1, 2); }

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026