Kids, Code, and Computer Science 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:

move_arm_to_ball_area
detect_ball_color
grip_ball
move_arm_to_bin_area
release_ball

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:

detect_arm_position

and

move_arm_by

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

Automate the Boring Stuff with Python

This book helps average non-technical people free themselves from mundane computer tasks.

Solar Cockroaches, Water Rockets, and Pinhole Eyeglasses

Here are a few of the many fun summer projects at Instructables.com website.

Bitsbox: Monthly Coding Projects in the Mail

Every month the Bitsbox service delivers a colorful booklet full of fun coding projects for kids to do then share with friends and family.

Simple and Fun 3D Models to Print

Here are a number of simple and fun 3D models to print.

FUZE Computer Teaches Kids to Code

The FUZE computer is a 1980s computer design with the brain of a modern Raspberry Pi. It's ideal for teaching kids to code plus build electronics projects.

An Interview with Paul Chayka

Paul Chayka talks about Robotics And Beyond, an after school tech program that teaches coding, robotics, circuit design, and much more.

Teach Your Kids to Code

Learning Python is a fun way to spend time and learn programming together.

Kano Computers

The Kano computer is a Raspberry Pi computer with a spiffy installation process (and user manual) and a carefully thought out online experience.

Functions and Procedures

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

Ruby

Ruby is a highly flexible programming language used in many large scale online applications at Twitter, Kickstarter, and other companies.

June 2015 Learn More Links

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

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

June 2015 News Wire

News and project stories about kids, coding, computer science, and how we use technology in our daily lives.

Create Art with the Paper App + Mix

The Mix community lets you find and share artwork to create projects with free Paper software.