A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
A void function performs an action without producing a value. It prints, writes, modifies, logs, signals. The caller does not need a result. The function's effect is observable through side effects: modified parameters, written files, displayed output. Void functions are the right choice for procedures, event handlers, cleanup routines, and any operation where the action matters more than the output.
#include <stdio.h>#include <string.h>void print_separator(void) {printf("================================\n");}void print_header(const char *title) {print_separator();printf(" %s\n", title);print_separator();}void swap(int *a, int *b) {int temp = *a;*a = *b;*b = temp;}void zero_array(int *arr, int n) {for (int i = 0; i < n; i++) {arr[i] = 0;}}int main(void) {print_header("System Report");int x = 10, y = 20;printf("Before swap: x=%d, y=%d\n", x, y);swap(&x, &y);printf("After swap: x=%d, y=%d\n", x, y);int data[] = {1, 2, 3, 4, 5};zero_array(data, 5);printf("Zeroed: ");for (int i = 0; i < 5; i++) printf("%d ", data[i]);printf("\n");return 0;}
None of these functions return a value. print_header displays text. swap modifies two variables through pointers. zero_array modifies an array. The effects are the point. The return type is void because there is nothing meaningful to return. Forcing a return value (returning 0 for success) would add noise without adding information. The caller knows the function succeeded because the side effect happened.
Void functions are procedures. They do things. They change state. They communicate through side effects, not return values. Use them for output, mutation, and cleanup. Use non-void functions for computation and queries. The distinction makes your API self-documenting. Next: legal ways to call void functions, including using them in comma expressions.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.