beanz Magazine

Functions and Procedures

Steve Jurvetson on Flickr

An introduction to programming functions and procedures using pen and paper instead of code. Includes lots of examples and exercises.

Editor’s Note: I recently took a class, Pen and Paper Coding, which teaches programming with pen and paper instead of computers and a specific language. Erik Linde, the founder and teacher, let me reprint one part of a class so you can experience this remarkable computer-free and language-free way to learn programming. This article — which has been modified from its original — teaches you about functions and procedures with exercises you can do to test your knowledge.

An introduction to procedures

Let’s say you are building software that controls an industrial robot. The robot has an arm with a grip, and by moving its arm and using the grip, it can sort a bin containing red and green balls, by putting each ball in a new bin that matches the ball’s color.

While this is an easy task for a human, it is a quite complex task for a computer. When writing your code, you might therefore benefit from splitting your code up into smaller chunks before you tackle everything. Such chunks are called procedures in programming. A procedure is a section of code that performs a specific task.

Splitting code into procedures

Let’s make an attempt at identifying the robot’s tasks — this will be our first step when we think about procedures. The robot should be able to:

  • Move its arm to the bin area containing the balls
  • Look at a ball, and figure out if it’s red or green
  • Grip a ball
  • Move its arm to the bucket area
  • Release a ball

In this case, it might be appropriate to write procedures for each of these tasks. Not only will this make our task more manageable, it will also allow us to test specific parts of our code and make sure it is working properly, before moving on to the next task.

Our procedures would need to be appropriately named, and they would need to exist for a very specific purpose. For example, for the above tasks, their corresponding procedure names might be:


Each of these procedures are actually still quite complex, so let’s see if we can break them down further. If we look at move_arm_to_ball_area, we can reason that a sophisticated robot would probably be equipped with both sensors and actuators (read: motors), to move its arm, and would need to be programmed to read its sensor values (to detect where the arm currently is), as well as code to move the motor by an amount appropriate to reach its destination. We could therefore break up move_arm_to_ball_area into smaller procedures, like so:




We could then of course break these procedures down further and further, until we have something small enough to actually write code for. When we have procedures that are small enough to write code for, we would write the code, and then test that code under many different scenarios to make sure it actually worked. We would then move on to the next procedure, write it, test it, etc.

The thinking of taking a big problem and breaking it down into small, manageable pieces, is a tried and tested way of programming, and is called top-down programming. It’s also a tactic we will use oftentimes in this article.

Code re-use and the DRY principle

Another benefit of separating your code into smaller procedures is that the code can be re-used in different locations in your code base. For example, the detect_arm_position would likely be used both by the move_arm_to_ball_area and then move_arm_to_bin_area procedures, and perhaps in many other locations as well.


Become a subscriber and get access to the rest of this article. Plus all our magazine articles.

Stories also include numerous links to help parents, kids, and teachers learn more. Get access today at just $15 per year!

Subscribe Today!

Also In The June 2015 Issue

October 2015 Issue: Internet of Things (IoT)

The Internet of Things (IoT) connects dumb devices like refrigerators to the internet and uses software to connect them to our daily lives.

There are many operating systems for internet of things devices, from existing software used to control electronic boards to efforts by Google and Apple.

Disney Infinity 3.0, Rocket League, and Super Mario Maker are three fun video games to consider for the 2015 holiday season

You can learn a little software programming and have lots of fun with any number of coding apps available for your phone or tablet computer.

Operating system software is a key part of all computers. But what are they and how do they work?

Learning how to make kittens with JavaScript is a great way to learn how to use the free Chrome web browser to practice and learn JavaScript.

Working through a book can help parents learn programming with their kids or kids learn on their own.

Board games and card games are some of the best ways to learn about programming. You don't need a computer. Play as a family or group.

These robots also can be programmed to move around rooms, one way for kids to learn programming.

Six women were hired to use their math skills to program the ENIAC computer. They called themselves The First Programmers Club.

Nicky is a Linguistics major who learned coding skills to further her research. She's also finished a PhD, won a few big awards, and co-founded Grok Learning.

Two women created an innovative online service to teach teenage girls how to code by using video.

The new Rust programming language is designed to solve problems with operating systems and fix issues with C and other languages.

Data can become alive and pose questions as well as reveal answers to questions we have.

Links from the bottom of all the articles in this issue, collected in one place for you to print, share, or bookmark.

DRY is an acronym for Don't Repeat Yourself. It's a critical programming concept and skill to learn.

Interesting news stories about computer science, software programming, and technology for August and September 2015.

CoderDojo is a free after school club for kids ages 7-17 where kids, parents, mentors, and others play with technology and learn to code.