Functions are self-contained blocks of code within a program. They have defined inputs and outputs, and they can be called from other parts of the code. Thus functions allow programmers to re-use a common block of code in different places within the program.
During her voyage to New Atlantis, Ann spent a good deal of time observing the ship's crew. After witnessing the ships' pointers coordinate the expansion of their convoy, she had become fascinated by all aspects of the ship’s operations. Ann spent hours watching as the sailors hoisted sails, tied off ropes, cleaned the decks, recorded headings, and checked the ocean depths. Driven by intense curiosity, Ann tried to learn as much as she could about each of these jobs.
One morning Ann asked the captain where she could learn more. Always eager to help, the captain disappeared into the ship's command cabin and returned with a 4,096 page Sailor's Manual (3rd edition). Ann was shocked.
"Why is it so large?" she asked.
"There is a lot to know," responded the captain with pride. "It takes the average sailor 5 years to learn the basics and another 35 to learn the rest of the manual."
"I see," Ann said. That seemed like a large cost to learn sailing. Then again, it probably took years to simply read through the manual itself. In Ann’s experience, operational manuals were rarely quick reads.
That night, Ann took the sailing manual out onto the deck. She opened to a random page and started reading. It was a section on how to tie the ship to the dock. The instructions occupied 3 full pages of tiny print. The level of detail was astounding. Each instruction spelled out every last aspect of the task. Ann read each line, trying to absorb the information. She even practiced the instructions for tying the ropes into knots (25 lines long) on her own shoe laces. After about half an hour, she felt that she actually had a pretty good understanding of the procedure.
The next section described how to tie a dingy to the side of the boat. Again, the section occupied 3 pages of tiny print. As Ann scanned the pages, she began to recognize instructions from the last section. In fact, the 25 lines about tying the rope were absolutely identical. She flipped back and forth rapidly between the two sections to confirm.
The next day, Ann approached the captain about this discovery.
"How observant!" he congratulated her. "Many of the concepts are very similar. For example, take ropes. There are only a few really good ways to tie knots."
"Why repeat the instructions line for line?" asked Ann.
"Because you need to do them each time. It would be horrible to skip the part about tying the knot. The dingy would drift off."
"I understand that. Why not just say something like: 'Tie the rope with a granny knot'. It would save you a lot of space -- 24 lines in this section alone. All that you need to do is to break out the common tasks into their own sets of instructions." suggested Ann.
"Wouldn't that make it much harder to understand the instructions?” asked the captain. “Right now, every step is laid out perfectly. I can hand any sailor the 35 pages about trimming the main sail without having to assume any prior knowledge."
"It would make it easier," argued Ann. "Once a sailor learns how to tie a specific knot, he or she could do it in any context. It would become a fundamental step itself… like a computer function."
"I guess it would make things shorter," agreed the captain. "But really, the length of the book is not a big problem. We also use the books for ballast, so having heavy books can be useful."
"It is better than that," argued Ann, ignoring the comment about ballast. "What if you find a better knot? Right now you have to change every single instruction that uses the current knot. It is probably a long and error prone process. If you are writing the same thing a hundred times, you will make a mistake one of those times. You probably have dozens of mistakes in here already.”
“What if you want to instruct a sailor to do something new, such as tie down a hot air ballon?” Ann continued. “Do you have instructions for that?"
"Well… no," admitted the captain.
"Wouldn't it be easier to say: 1) Grab the rope, and 2) Tie it to the port railing with a granny knot. Those are instructions you can even give verbally. The sailor only needs to know how to perform a few basic functions to start."
The captain fell silent and stared out into the sea. After a moment he nodded slowly.
"I think you are correct!" he declared loudly. After three days at sea with him, Ann had realized that the captain was a big fan of declaring things loudly.
"I bet it goes beyond simply tying ropes. The signals that we use to communicate can be broken off into functions too.” The captain trailed off as he slowly looked around the ship, assessing the resuse of different skills.
Suddenly, he spun back toward Ann. “Will you help me rewrite the book? We could break things up into these standalone units that you describe. It would revolutionize the entire sailing industry!"
Ann's smile faltered. While she could not resist an opportunity to improve the performance of the entire royal navy, she was still on her quest to save the kingdom. She weighed the options in her head. Finally, with much regret, she declined the offer. However, since she believed deeply in the value of the effort, she wrote a request to her father to send six of the kingdom's top computational thinkers to aid the effort. Satisfied that together they would be as helpful as herself, Ann let her mind wander to other questions.