A procedural module groups related functions and keeps their implementation details and working state within a defined scope. Its callers use the public operations directly. The module takes responsibility for the machinery needed to carry them out.
For example, an application's audio system can expose a small API:
Audio.initialize()
Audio.setVolume(0.5)
Audio.play(notification)
The caller does not need to create an audio object, store it, or forward it to another component. Once the application has initialized the module, any code allowed to use it can request playback.
The examples here use language-neutral pseudocode. A possible implementation is:
module Audio:
private device
private volume = 1.0
initialize():
device = openAudioDevice()
setVolume(value):
volume = clamp(value, 0, 1)
play(sound):
device.play(sound, volume)
stop():
device.stop()
The device and volume are shared by this module's functions. Other code operates on them through the public API. The scope might be enforced by a language module, a file, or another encapsulation mechanism; the design does not depend on a particular syntax.
Its appeal is that the module settles some decisions once. The application has one audio environment, and callers can use it without carrying a reference to that environment through their own construction code. This can make a system substantially easier to use when shared access matches its requirements.
The public functions already form an interface. Their contract describes what each operation does, which inputs it accepts, what can fail, and any requirements about ordering or initialization. A separate interface declaration is one way to represent a contract in a language, but the abstraction boundary already exists between these functions and their callers.
For example, the module could replace its audio library while preserving Audio.play(sound). Calling code would remain unchanged if the observable contract remained compatible. Its independence comes from what the API hides, rather than from whether the API is expressed through a class.
Direct access does fix a dependency choice: a call to Audio.play selects that module. This is different from a function that receives a playback operation from its caller. Both can hide implementation details; the latter allows the caller to choose the provider for that use.
A procedural module's functions can themselves be injected. A consumer can receive Audio.play as a parameter, or a function-table struct can describe a group of operations:
struct AudioAPI:
play: fn(Sound) -> void
stop: fn() -> void
audio = AudioAPI {
play = Audio.play,
stop = Audio.stop
}
Here, fn(...) -> ... describes a function-pointer type. The table maps its fields to module functions. The module is the organization of the implementation. Dependency injection is a way to connect consumers to it. They are compatible choices.
That leaves a practical question: where should the program make the choice? A reusable playback widget may need to receive its audio operations, while an application-specific notification handler may reasonably call the application's audio module directly. Different boundaries can make different decisions.
There is still a cost to making dependencies explicit. Intermediate components sometimes carry dependencies solely to pass them to other components. A change to the required dependencies can then propagate through several layers of construction code.
For example, a button may need audio even though the toolbar and editor that construct it never use audio themselves:
createEditor(audio: AudioAPI):
return createToolbar(audio)
createToolbar(audio: AudioAPI):
return createSaveButton(audio)
createSaveButton(audio: AudioAPI):
return Button(onClick = () => audio.play(clickSound))
Using a function parameter instead of a table narrows the dependency, but the forwarding remains:
createEditor(playSound):
return createToolbar(playSound)
createToolbar(playSound):
return createSaveButton(playSound)
createSaveButton(playSound):
return Button(onClick = () => playSound(clickSound))
If the button later needs an additional operation, the construction path may need to change too. Adding a field to an existing table can avoid changing every forwarding function's signature, but the code that constructs the table must still supply the new dependency. Passing a broad application-services table can reduce signature changes further, at the cost of making each consumer's actual requirements less explicit.
Directly calling Audio.play inside the button would remove this forwarding. It would also make the button select that particular module instead of allowing its caller to select the implementation. Whether that is appropriate depends on the intended reuse of the button and the application. An application-specific button may reasonably make that choice; a reusable component may need to leave it open.
This consideration applies to class-based injection, function parameters, and function tables alike. The function-pointer approach makes the representation straightforward, while the architectural decision remains: which boundaries need caller-controlled selection, and which dependencies can be settled within a module? Keeping that choice deliberate preserves convenient module usage without giving up injection where it is useful.
Shared state is another independent consideration. A private variable is protected from arbitrary external access, but everyone using the module still observes its effects:
// The settings screen changes the application's volume.
Audio.setVolume(0.1)
// Notifications now use that volume too.
Audio.play(newMessage)
For application-wide audio settings, that relationship can be intentional. For a preview player and an editor that need separate volumes, it can be a mistake. Encapsulation controls access to state; it does not make the state independent for each caller.
One option is to keep the functions while moving per-session state into a context:
preview = Audio.createContext()
editor = Audio.createContext()
Audio.setVolume(preview, 0.1)
Audio.setVolume(editor, 1.0)
Audio.play(preview, previewSound)
Audio.play(editor, notification)
Each context now holds its own settings and playback state. The API stays procedural, while the caller explicitly selects which state to use. The contexts may still share underlying hardware. Independent application state does not require separate physical devices.
Passing an implementation and passing state solve related but distinct problems. Two function tables pointing at the same module-level functions still share whatever state those functions use. Independent consumers need independent contexts, captured state, or another explicit ownership mechanism.
Module access also leaves lifecycle responsibilities to be designed. The application should know when initialization happens, whether it can fail, and when resources are released. A convenient global entry point does not make an uninitialized device valid. Likewise, shared mutable state needs appropriate synchronization when used concurrently; synchronization alone does not give different consumers separate settings.
Testing makes these choices visible. Code that directly calls a real audio module depends on the environment that module requires. A test can configure a substitute backend for the whole module, but concurrent tests would then share that configuration. Locally supplied functions or independent contexts let tests select different environments without changing one another's dependencies.
A module can also accept its backend once at startup while keeping its callers simple:
Audio.initialize({
play = PlatformAudio.play,
stop = PlatformAudio.stop
})
// Callers use the configured system directly.
Audio.play(notification)
This injects a dependency into the module. It makes the backend configurable for that module instance, while ordinary callers still use the same shared selection. Per-call selection would require a different boundary. The distinction is useful when choosing between application-wide configuration and independently reusable subsystems.
A practical module has a small, coherent API, contains its implementation details, and gives shared state a deliberate scope and lifecycle. Direct calls are useful when the application can settle a dependency choice once. Function parameters, function tables, and contexts expose the choices consumers need to control. The design can keep ordinary usage simple while preserving explicit substitution at the boundaries that require it.