Skip main navigation

Abstraction

Breaking down code into reusable chunks can make it easier to understand. Doing so can hide background details and remove unnecessary information.

When you write programs, the code can easily become long, complex, and hard to read. So that you can break it down into smaller, more understandable chunks, programming languages let you create your own commands which you can reuse throughout your code.

Using abstraction

Imagine you want to write a program which makes a robot wave.

An animation of a cartoon robot waving, by lifting up its arm, opening its hand, moving its arm left, right, left, right, closing its arms, and then lowering its hand.

To make the robot wave, you could give it the following instructions:

lift up your arm
open your hand
move your arm left
move your arm right
move your arm left
move your arm right
close your hand
lower your arm

That would be a lot of instructions to give each time we wanted to make our robot wave. It would be much simpler if we could teach it how to wave once, and each time after that we could simply give it the instruction wave. In programming this is known as ‘abstraction’. Abstraction is the act of removing complexity from a problem by hiding background detail and removing unnecessary information.

how to wave:
lift up your arm
open your hand
move your arm left
move your arm right
move your arm left
move your arm right
close your hand
lower your arm

wave
wave
wave

Most programming languages allow you to create a set of commands and label them. Using that label, you can then run that set of instructions from elsewhere in your program. These sets of instructions are often called a ‘subroutine’, ‘procedure’, or ‘function’. In Scratch, you can create your own subroutines using my blocks.

Next step

In the next step you will create your own block to draw a shape. When you do this, you will be abstracting a sequence of commands into one new command.

This article is from the free online

Introduction to Programming with Scratch

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education