C Functions

C Tutorial

🧩 C Functions (Complete Tutorial: Beginner → Advanced)

In C language, functions are the building blocks of a program.
They help you divide a large problem into smaller, reusable parts, making code clean, readable, and maintainable.


1️⃣ What is a Function in C?

A function is a self-contained block of code that performs a specific task and can be called whenever needed.

Example

printf("Hello World");

printf() is a built-in function.


2️⃣ Why Use Functions?

✔ Code reusability
✔ Better readability
✔ Easy debugging
✔ Modular programming
✔ Team-based development


3️⃣ Types of Functions in C

1. Library Functions

Predefined functions provided by C

printf(), scanf(), sqrt(), strlen()

2. User-Defined Functions

Functions written by the programmer


4️⃣ Basic Structure of a Function


5️⃣ Function Declaration (Prototype)

Tells the compiler function name, return type, and parameters.

int add(int, int);

✔ No body
✔ Usually written before main()


6️⃣ Function Definition

Contains the actual code.

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

7️⃣ Function Call

Calling a function from main() or another function.

int result = add(10, 20);

8️⃣ Complete Example Program ⭐


 


9️⃣ Types of Functions (Based on Arguments & Return)

1️⃣ No arguments, no return

void greet() {
printf("Hello");
}

2️⃣ Arguments, no return

void print(int x) {
printf("%d", x);
}

3️⃣ No arguments, return value

int getNumber() {
return 10;
}

4️⃣ Arguments and return value ⭐

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

🔟 Call by Value (Default in C)

void change(int x) {
x = 100;
}

✔ Original variable not changed


1️⃣1️⃣ Call by Reference (Using Pointers)

void change(int *x) {
*x = 100;
}

✔ Original variable is modified


1️⃣2️⃣ Recursive Functions

Function calling itself.


1️⃣3️⃣ Inline Functions

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

✔ Faster execution
✔ Small functions only


1️⃣4️⃣ Function Pointer (Advanced)


 

✔ Used in callbacks & libraries


1️⃣5️⃣ Functions in Multi-File Programs

  • .h → function declaration

  • .c → function definition

// math.h
int add(int, int);

🔟6️⃣ Common Mistakes ❌

❌ Missing function prototype
❌ Mismatch in parameters
❌ Forgetting return value
❌ Using global variables unnecessarily


📌 Interview Questions (Very Important)

  1. What is a function?

  2. Difference between declaration & definition

  3. Types of functions in C

  4. Call by value vs call by reference

  5. What is recursion?

  6. What is function pointer?


🔥 Real-Life Use Cases

  • Operating systems

  • Embedded systems

  • Game engines

  • Scientific applications

  • Large software projects


✅ Summary

✔ Functions make programs modular
✔ Improve readability & reusability
✔ Essential for DSA & system programming
✔ Must-know for placements & interviews

You may also like...