Lesson 10 / 36
Function Basics
Package reusable logic into named blocks.
Why functions?
A function is a named block of code you can call repeatedly instead of copy-pasting. It takes inputs (parameters), does work, and can send back a result (return value).
Anatomy of a function
Every function has a return type, a name, a parameter list, and a body.
int add(int a, int b) {
int sum = a + b;
return sum;
}
int main(void) {
int result = add(3, 4);
printf("%d\n", result);
return 0;
}
Output:
7
void means nothing
A function that returns nothing uses void as its return type. void greet(void) { printf("Hi\n"); }