Module chapter opener illustration

Module

FUNCTION / ENCAPSULATION — *does one job well and can be called anywhere.*

Listen along — Module

Loading audio…

Press play to listen along. The line being read lights up as you go.

Show full transcript

Loading transcript…

Chapter 4 — Module and the Labeled-Box Function

The workshop hummed with the low thrum of half-finished projects and the quiet murmur of students trying to untangle their latest coding challenges. Loop stood at the front, a wide, encouraging smile on her face. Today, she wasn’t just talking about abstract concepts. Today, she had brought in a physical representation, something tangible to grasp. She carefully placed a small, brightly painted box on the central display table.

This wasn’t some cute animal mascot or a miniature person. No, this was a deliberately abstract concrete-object-figure: a simple, rectangular box, painted a cheerful, unassuming green. On its left side, a neat slot yawned open, clearly marked “INPUT.” On the right, another identical slot waited, labeled “OUTPUT.” Across the front of the box, a blank white space invited a label.

“Alright, everyone,” Loop announced, her voice cutting through the workshop’s background noise. “Meet Module.”

She paused, letting the students lean in, their curiosity piqued. Module sat there, silent and unassuming, yet somehow radiating importance.

“Module,” Loop continued, picking up a dry-erase marker, “is here to show us something fundamental about how we build programs. It embodies the idea of a function.”

She wrote “SQUARE NUMBER” neatly on Module’s label.

“Think of Module as a specialized worker,” Loop explained. “It has one job, and it does that job perfectly every single time.” She held up a small, smooth wooden cube, marked with the number ‘3’. “This,” she said, indicating the cube, “is our input. We drop it into the left slot.”

The cube clattered softly as it disappeared into the “INPUT” slot. A moment later, with a soft whirring sound from inside the box, a new cube, this one marked ‘9’, emerged from the “OUTPUT” slot.

A few students gasped. Others simply nodded, already connecting the dots.

“So, what just happened?” Loop asked, her gaze sweeping across the room.

A girl in the front row, Maya, raised her hand. “It squared the number? Three times three is nine.”

“Exactly!” Loop beamed. “Module took an input, performed its specific job, and returned an output. That, in a nutshell, is what a function does.”

She picked up another cube, this one marked ‘5’. “What do you think will happen if I put this in?”

“Twenty-five!” several students called out.

Loop dropped the ‘5’ cube in. The whirring sound returned, and out popped a ‘25’ cube.

“See?” Loop said. “Same job, different input, predictable output. Functions have a name—like ‘Square Number’ here. They take inputs, which we sometimes call parameters or arguments. Inside, they have a body, which is the actual job they do. And finally, they produce an output.”

She gestured to the box. “Notice anything else? Can you see how it squared the number?”

The students squinted at the box, then shook their heads. The green sides were solid, opaque.

“That’s the magic of encapsulation,” Loop clarified. “The caller—that’s us, putting in the numbers—knows what the function does: it squares the number. But we don’t need to know how it does it. Maybe there’s a tiny calculator inside, or a series of gears. The internal implementation is hidden. All we care about is that it works.”

This hiding of the inner workings, she explained, made things much simpler. You didn’t need to worry about the complex steps involved; you just trusted the function to do its job. It was like using a blender: you knew it would turn fruit into a smoothie, but you didn’t need to understand the motor’s wiring every time you pressed the button.

“And the best part?” Loop continued, her eyes sparkling. “You define a function once, and you can call it anywhere.” She picked up Module and placed it on a different table. “It doesn’t matter where it is. As long as you give it an input, it will do its job. This saves us from repeating ourselves, which we call the DRY principle: Don’t Repeat Yourself.”

Loop then introduced a second, identical green box, but this one was labeled “ADD 10.” She placed it next to the “SQUARE NUMBER” Module.

“Functions can also call other functions,” she said, demonstrating. She fed a ‘2’ into “SQUARE NUMBER,” waited for the ‘4’ to emerge, then immediately fed that ‘4’ into “ADD 10.” Out came a ‘14’.

“This is called composition,” Loop explained. “We’re building a bigger task by chaining together smaller, simpler jobs. First, square the number. Then, add ten to the result.” It was like building with LEGO bricks, each brick a complete, functional piece that could connect to others.

She then talked about “pure functions.” “Imagine our ‘Square Number’ Module,” she said. “If I put in a ‘3’, it always gives me a ‘9’. It never gives me a ‘7’ one time, or suddenly changes the color of the table. It just does its one job, perfectly and predictably, without any unexpected ‘side effects’ on the world around it. That’s a pure function—same input, same output, every single time, with no surprises.”

Loop then wrote a few more labels on blank cards: calculateTotal(), isValid(), getData().

“When we name our functions,” she advised, “we want to be clear. Use a verb or a question. calculateTotal() tells you it’s going to do something to calculate a total. isValid() asks a question: Is this valid? But getData()? That’s not quite right. It’s too vague. What kind of data? What are you doing with it?” She crossed out getData(). “Be precise. Clear names make your code much easier to understand.”

Finally, Loop touched on a more advanced idea. “Sometimes, a function needs to call itself. This is called recursion.” She held up Module again. “Imagine ‘Square Number’ had a job that involved breaking down a really big number, and part of that job was to square a smaller piece of that number. It would essentially tell itself, ‘Hey, Square Number, do your thing on this smaller piece!’ It’s like a set of Russian nesting dolls, where each doll contains a smaller version of itself, doing the same basic job on a smaller scale.”

The students looked thoughtful, some still wrestling with the idea of a box calling itself.

“The main takeaway,” Loop concluded, returning Module to the center of the table, “is that breaking large programs into small, clear functions, each with a small, clear job, makes everything more manageable. That’s modularity. Module helps us build complex things, one simple, predictable step at a time.”

Module sat there, a silent, green testament to organization and efficiency, its slots waiting for the next input, ready to perform its single, perfect job.


The CodeRealm ensemble

Module is part of CodeRealm's distributed-narrative cast. Each character embodies a different curricular primitive; together they teach the full subject.