C Functions
Challenge Gallery
Quick Reference
Function definition pattern:
// Prototype (declaration)
int add(int a, int b);
// Definition
int add(int a, int b) {
return a + b;
}
void functions:
void greet(const char *name) {
printf("Hello, %s\n", name);
// no return value needed
}
Common Pitfalls
- Missing prototype – Calling a function before declaring it causes implicit declaration warnings (or errors in C99+).
- Forgetting pass-by-value – Changes to parameters don’t affect the caller. Use pointers to modify the caller’s data.
- Missing return – Non-void functions must return a value on all paths. Use
-Wallto catch this. - Returning local addresses –
return &local_var;is a dangling pointer. The variable is destroyed when the function returns.