Nearby lessons

72 of 124

C - Function Declaration

A function declaration — also called a prototype — tells the compiler a function's name, return type and parameter types before it is used. Learn the syntax, where prototypes go, and what breaks without them.

The Prototype Syntax

A declaration is the function's first line followed by a semicolon instead of a body:

Example01
CCode Cell
1#include <stdio.h>
2 
3/* Declarations - what exists, not how it works */
4int add(int a, int b);
5void greet(char name[]);
6int factorial(int n);
7 
8int main()
9{
10 printf("add(3, 4) = %d\n", add(3, 4));
11 printf("factorial(5) = %d\n", factorial(5));
12 greet("Rahul");
13 return 0;
14}
15 
16/* Definitions - the actual code */
17int add(int a, int b) { return a + b; }
18 
19void greet(char name[]) { printf("Hello, %s!\n", name); }
20 
21int factorial(int n)
22{
23 int i, result = 1;
24 for (i = 2; i <= n; i++) result *= i;
25 return result;
26}
Output
add(3, 4)      = 7
factorial(5)   = 120
Hello, Rahul!

Why the Compiler Needs It

In simple words: the C compiler reads your file top to bottom, once. When it reaches add(3, 4) it must already know that add takes two ints and returns an int — otherwise it cannot check your arguments or generate the right code. The prototype supplies that knowledge early.

Without a prototype, the call is an error in modern C:

Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 /* ERROR in C99 and later:
6 'add' undeclared (first use in this function) */
7 printf("%d\n", add(3, 4));
8 return 0;
9}
10 
11int add(int a, int b) { return a + b; }
Output
error: implicit declaration of function 'add'

Parameter Names Are Optional

Only the types matter to the compiler. Names are for human readers — and worth including for exactly that reason:

Example03
CCode Cell
1#include <stdio.h>
2 
3/* All three declare the same function */
4int divide(int numerator, int denominator); /* clearest */
5/* int divide(int, int); */ /* also legal */
6/* int divide(int x, int y); */ /* names need not match */
7 
8int main()
9{
10 printf("20 / 4 = %d\n", divide(20, 4));
11 return 0;
12}
13 
14int divide(int a, int b) /* definition may use other names */
15{
16 return b != 0 ? a / b : 0;
17}
Output
20 / 4 = 5

Use (void) for No Parameters

Empty parentheses mean "unspecified parameters" in older C, which disables argument checking. (void) means "definitely none":

Example04
CCode Cell
1#include <stdio.h>
2 
3int getNumber(void); /* correct: takes nothing */
4/* int getNumber(); loose: no checking in old C */
5 
6int main()
7{
8 printf("Value: %d\n", getNumber());
9 /* getNumber(5); with (void) this is a compile error - good */
10 return 0;
11}
12 
13int getNumber(void) { return 42; }
Output
Value: 42

Declaration vs Definition

Declaration (prototype)Definition
Ends withA semicolonA body in braces
Contains codeNoYes
How many allowedMany (identical)Exactly one
Reserves memoryNoYes
Typical locationTop of file or a headerBelow main or another .c file
Example05
CCode Cell
1#include <stdio.h>
2 
3int square(int n); /* declaration - repeating it is harmless */
4int square(int n);
5 
6int main()
7{
8 printf("square(9) = %d\n", square(9));
9 return 0;
10}
11 
12int square(int n) /* definition - only one allowed */
13{
14 return n * n;
15}
Output
square(9) = 81

When You Can Skip the Prototype

If a function is defined above its first use, the definition itself acts as the declaration. This works, but stops scaling once functions call each other:

Example06
CCode Cell
1#include <stdio.h>
2 
3int add(int a, int b) /* defined before use - no prototype needed */
4{
5 return a + b;
6}
7 
8int main()
9{
10 printf("%d\n", add(2, 3));
11 return 0;
12}
Output
5

Mutual Recursion Needs Prototypes

When two functions call each other, no ordering works without a prototype — one of them must be declared first:

Example07
CCode Cell
1#include <stdio.h>
2 
3int isOdd(int n); /* forward declaration is essential here */
4 
5int isEven(int n)
6{
7 if (n == 0) return 1;
8 return isOdd(n - 1);
9}
10 
11int isOdd(int n)
12{
13 if (n == 0) return 0;
14 return isEven(n - 1);
15}
16 
17int main()
18{
19 printf("7 is %s\n", isEven(7) ? "even" : "odd");
20 printf("10 is %s\n", isEven(10) ? "even" : "odd");
21 return 0;
22}
Output
7 is odd
10 is even

Prototypes in Header Files

To share functions across .c files, put the declarations in a .h file and include it everywhere. The include guard prevents duplicate declarations:

Example08
CCode Cell
1/* ---------- mathutils.h ---------- */
2#ifndef MATHUTILS_H
3#define MATHUTILS_H
4 
5int add(int a, int b);
6int multiply(int a, int b);
7 
8#endif
9 
10/* ---------- mathutils.c ---------- */
11#include "mathutils.h"
12 
13int add(int a, int b) { return a + b; }
14int multiply(int a, int b) { return a * b; }
15 
16/* ---------- main.c ---------- */
17#include <stdio.h>
18#include "mathutils.h"
19 
20int main()
21{
22 printf("add : %d\n", add(4, 5));
23 printf("multiply : %d\n", multiply(4, 5));
24 return 0;
25}
Output
add      : 9
multiply : 20

Common Mistakes

  • Forgetting the semicolonint add(int a, int b) without ; starts a definition and produces a confusing error.
  • Mismatched types — declaring int f(int) then defining float f(float) is a conflicting-types error.
  • Empty parenthesesint f(); disables argument checking; write int f(void);.
  • Defining a function twice — a duplicate definition is a linker error, unlike a duplicate declaration.
  • Putting definitions in a header — every file that includes it gets a copy, causing multiple-definition errors.
Headers declare; source files define. A .h file should contain prototypes, typedefs and macros — never function bodies. Put a body in a header and any project with two .c files including it will fail to link.
📝 Key Takeaways
  • Syntax: returnType name(parameterTypes); — note the semicolon.
  • Parameter names in a prototype are optional documentation.
  • A prototype lets you define functions after main().
  • Use (void) for no parameters, not empty parentheses.
  • Header files collect prototypes for sharing between .c files.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4