C Programming • C17 baseline, standards-specific features labeled
Create and Use C Functions
Give each function a clear contract expressed through its parameters and return type.
C passes arguments by value. A function that needs to update a caller-owned object receives a pointer to it. Keep functions focused, validate inputs where appropriate, and use a return value to report success or failure.
Example
#include <stdbool.h>
#include <stddef.h>
bool divide(double numerator, double denominator, double *result) {
if (result == NULL || denominator == 0.0) {
return false;
}
*result = numerator / denominator;
return true;
}
What to expect
The function returns false for a null output pointer or a zero denominator; otherwise it stores the quotient and returns true.
Key idea: Document pointer parameters, including whether null is allowed and whether the function writes through them.