Definition
📌 Functions
A function is a reusable, named block of code that performs a specific task.
Syntax
syntax
returnType functionName(parameterType param) {
// code
return value;
}Example
c
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(4, 7);
printf("Sum: %d\n", result);
return 0;
}Output
outputSum: 11
Important Points
- Functions must be declared (or defined) before they're used, or you need a prototype at the top of the file.
- Parameters are passed by value in C by default — the function gets a copy, not the original.
- Use pointers as parameters if a function needs to modify the caller's variable directly.
- A function can have at most one return value, but it can return a pointer or struct to carry more data.
Common Mistakes
- Forgetting a function prototype when the function is defined after
main(). - Expecting a function to change a variable passed by value in the caller.
- Mismatching the declared return type and what's actually returned.
Interview Tip
🎯 Before your interview
Be ready to explain "pass by value vs pass by reference" in C — C only has pass by value, but you can simulate pass by reference using pointers.
