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…
Module wasn’t like a regular pet. It wasn’t furry or feathery. Module was a box. A small, painted box, to be exact. It had a label on its front. The label told you what Module did. On its left side, there was a slot. That was for inputs. On its right side, another slot waited. That was for outputs. Loop always said, “Module does one thing. It does it well.”
“Alright, class,” Loop announced. He clapped his hands together. “Today, we meet Module.” Module sat on a little stand. Its label read: Add_Sparkle. “See this?” Loop pointed. “This is Module’s name. It tells you its job.” He held up a plain, grey pebble. “This is our input.” Loop dropped the pebble into Module’s left slot. Thunk. A soft whirring sound came from inside the box. It wasn’t loud. It sounded like a tiny, happy bee. Then, ping! A pebble rolled out of the right slot. It wasn’t grey anymore. This pebble shimmered. It had tiny, rainbow sparkles all over it. “Output!” Loop cheered. “A sparkly pebble!”
“Module does one job,” Loop explained. “It adds sparkles. Nothing else. It won’t bake you a cake. It won’t teach a dog to fetch.” He picked up another plain pebble. “What do you think will happen?” “Sparkles!” someone shouted. Loop grinned. He dropped the pebble in. Thunk. Whirr. Ping! Another sparkly pebble appeared. “Exactly!” Loop said. “Same input, same output. Every time.” “Now, here’s the cool part,” Loop continued. “You can use Module anywhere. Not just here. You could take Module to the park. Or to the moon! As long as it gets an input, it will give you an output.” He mimed putting Module in a backpack. “It’s ready whenever you need it.”
“Look closely at Module,” Loop said. “Can you see inside?” Everyone squinted. “Nope!” “That’s the magic!” Loop declared. “You don’t need to know how Module adds sparkles. You just need to know that it adds sparkles. The inside stuff is hidden. It’s Module’s secret.” “Why is that good?” Loop asked. He paused for effect. “Because if I wanted to change Module’s secret sparkle-adding machine, you wouldn’t even know! You’d still drop in a pebble. You’d still get a sparkly pebble out.” “It just works,” a kid whispered. “Exactly!” Loop beamed. “It just works. That’s a good function.”
“Imagine you have a big job,” Loop said. “Like making a whole galaxy of sparkly planets.” He waved his arms dramatically. “That’s a lot of sparkles!” “You could try to do it all at once. But that would be messy. And hard.” “Instead, you break it down. You use Module for one part. Add sparkles to one planet. Then you do the next planet. And the next.” “Each small job makes the big job easier.”
“Think about it,” Loop said. “If we needed to add sparkles to a thousand pebbles, would you want to build a new sparkle-machine for each one?” “No way!” “Exactly! That’s silly. You build Module once. You give it its instructions once. Then you can use it a thousand times. Or a million!” He pointed to the label. “That name, Add_Sparkle, means we can call it whenever we want. We don’t have to write out all the sparkle-adding steps again. We just say, ‘Hey, Module! Add_Sparkle this!’”
“What if we wanted a super sparkly pebble?” Loop wondered aloud. He picked up a pebble that already had some sparkles. It was an output from Module. “This pebble is sparkly,” he said. “But what if we wanted more sparkles?” He dropped the already sparkly pebble into Module’s left slot. Thunk. Whirr. Ping! The pebble that came out was blinding. It practically glowed. “See?” Loop explained. “Module did its job. Then we used Module again on the result. One function can use another function’s output. Or even call another function from inside itself!” He winked. “That’s how you build really amazing things.”
“One last thing about names,” Loop said. He tapped Module’s label. “Add_Sparkle. See how it’s a doing-word?”
“Like ‘run’ or ‘jump’?” a kid asked.
“Exactly!” Loop nodded. “A function’s name should tell you what it does. Or what question it answers.”
He wrote on a whiteboard: calculateTotal(). “This calculates a total.”
He wrote: isValid(). “This asks, ‘Is it valid?’”
“You wouldn’t name Module just ‘Pebble’,” Loop explained. “Because ‘Pebble’ doesn’t tell you its job. It just tells you what it works on.”
“So, doing-words or questions,” he summarized. “That makes it clear.”
Loop smiled. “So, remember Module. It’s a function.” He held up his fingers. “Inputs in. Output out. One job. Call it anywhere.” He repeated it slowly. “Not hard. One job. Inputs in. Output out. Call anywhere.” “And it keeps its secrets,” he added with a wink. “That’s encapsulation.” “It helps you break big problems into small pieces,” Loop finished. “That’s modularity.” “And you only build it once, but use it many times. That’s being smart!”
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.
-
Stash
Variable / storage — the labeled box that holds a value until you call for it
-
Fork
Conditional / branching — chooses a path based on what's true right now
-
Trek
Loop / iteration — keeps going around until the work is done
-
Glitch
Debugging / inspection — finds bugs gently, never shaming; 'there's always a reason'
-
Order
Sequence / syntax — reminds you that order matters in code
-
Row
A list: many values lined up in a numbered row, so you can grab item number three instantly or walk through them one by one.
-
Port
Input and output: the doorway that brings information in from the world (a key press, a sensor) and sends results back out.
-
Ping
An event: a waiting bell that does nothing until its trigger happens, then runs its code the instant it is struck.
-
Shuffle
Randomness: a fresh unpredictable value each time — a dice roll, a shuffled deck — so a program can surprise, vary, and stay fair.