C Programming - Beginner to Brilliance / Functions in C

Function Declaration & Definition

A function declaration tells the compiler what a function looks like before it is used. It includes the function name, return type, and parameter list, but no body.

This is also called Prototype.

Think of it like announcing the recipe without showing the full instructions yet.

int add(int, int);  // This is a declaration
  • int → Return type (the function will return an integer)

  • add → Name of the function

  • (int, int) → Function expects two integers as input

🧠 Why declare a function?

  • So the compiler knows:

    • What the function returns

    • How many arguments it takes

    • The type of those arguments

  • This prevents "implicit declaration" errors

✅ Where do we write it?

  • Usually at the top of the file, before main()

  • OR in a header file (.h file)


🔧 What is a Function Definition?

A function definition contains the actual code (the body) that runs when the function is called.

It tells the compiler how the function works.

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

Here:

  • int → Return type

  • add → Name of function

  • (int a, int b) → Input parameters

  • { return a + b; } → Function body that returns the sum


📚 Complete Example: Declaration + Definition + Call

#include <stdio.h>

// 🔔 Function Declaration
int add(int, int);

int main() {
    int result = add(5, 3);   // 🧑‍💻 Function Call
    printf("Sum = %d", result);
    return 0;
}

// 🧱 Function Definition
int add(int a, int b) {
    return a + b;
}
Sum = 8

💡 Here:

  • You declare add() before main()

  • You define it after main()

  • You call it from main() using arguments 5 and 3


❓ What Happens If You Don't Declare the Function?

In older versions of C, if you call a function without declaring it, the compiler would assume its return type is int, which is dangerous and error-prone.

In modern C (C99 and above), not declaring a function will result in a compiler error:

error: implicit declaration of function 'add'

✅ Always declare a function before using it, if the definition comes after main().


🧠 Function Declaration vs Definition: Analogy

Concept

Real-Life Analogy

Function Declaration

Saying "I'm going to bake a cake!"

Function Definition

Actually showing the full recipe and baking

Function Call

Actually baking a cake using that recipe


📚 Another Example: Multiple Declarations

You can declare multiple functions at the top, and define them below:

#include <stdio.h>

// Declarations
int square(int);
void printHello();

int main() {
    printHello();
    printf("Square of 4 = %d", square(4));
    return 0;
}

// Definitions
void printHello() {
    printf("Hello!\\n");
}

int square(int x) {
    return x * x;
}

🛡️ Tips & Best Practices

✅ Always declare functions before main() if their definition comes later

✅ Keep declaration and definition consistent (same number of parameters and types)

✅ Use descriptive names: e.g., calculateArea() instead of calc()

✅ Use comments to describe what the function does


📄 Summary Table

Concept

Declaration

Definition

Contains Body?

❌ No

✅ Yes

Ends with Semicolon?

✅ Yes

❌ No (has {} block)

Tells the Compiler...?

What the function looks like

What the function does

Required Before main()?

✅ Yes, if defined later

❌ No, can be placed anywhere


💡 Bonus: Declaration in Header Files

You can store declarations in a header file (.h) and definitions in .c file.

🔸 mathlib.h:

int square(int);

🔸 mathlib.c:

int square(int x) {
    return x * x;
}

🔸 main.c:

#include <stdio.h>
#include "mathlib.h"

int main() {
    printf("Square = %d", square(6));
    return 0;
}