C Functions

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.
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() is a built-in function.
Why Use Functions?
- Code reusability
- Better readability
- Easy debugging
- Modular programming
- Team-based development
Types of Functions in C
1. Library Functions
Predefined functions provided by C
2. User-Defined Functions
Functions written by the programmer
Basic Structure of a Function
Function Declaration (Prototype)
Tells the compiler function name, return type, and parameters.
- No body
- Usually written before
main()
Function Definition
Contains the actual code.
Function Call
Calling a function from main() or another function.
Complete Example Program
Types of Functions (Based on Arguments & Return)
No arguments, no return
Arguments, no return
No arguments, return value
Arguments and return value
Call by Value (Default in C)
- Original variable not changed
Call by Reference (Using Pointers)
- Original variable is modified
Recursive Functions
Function calling itself.
Inline Functions
- Faster execution
- Small functions only
Function Pointer (Advanced)
- Used in callbacks & libraries
Functions in Multi-File Programs
.h→ function declaration.c→ function definition
Common Mistakes
- Missing function prototype
- Mismatch in parameters
- Forgetting return value
- Using global variables unnecessarily
Interview Questions (Very Important)
What is a function?
Difference between declaration & definition
Types of functions in C
Call by value vs call by reference
What is recursion?
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
