Functions
A function is a reusable, named block of code that performs a specific task. Functions help you avoid repeating code and make programs easier to read and test.
A C function has a return type, a name, a parameter list, and a body.
💡 Key insight
A function that returns nothing uses the special return type <code>void</code>.
Syntax
syntax
returnType functionName(parameterType param) {
// code
return value;
}Example
add takes two int parameters and returns their sum, which main() then prints.
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;
}outputSum: 11
Try it yourself
Copy this example into the C compiler and run it — try changing a value and see what happens.
