In addition to using the ready-made blocks, you can make your own blocks. This lets you combine multiple blocks to perform a useful task. You can then use your task multiple times in your program. This technique helps you organize your programs as they get bigger.

Creating tasks

You create a new task by placing an empty Task block on the canvas, as shown below. You can drag one or more blocks into it that together make up your desired task. For example, you can make a task that prints “Hello World” with two pauses.

Task blocks
Drag a new Task block
onto the canvas.
Main program.
Call your task
in your program.
The Task Caller block runs
all the blocks of your task.
Add actions
to your task.
Choose a name
and optional icon
Tasks combine multiple blocks to do something useful. This can help you organize your code.

You can give your task any name, but it’s best to choose something brief and descriptive. You can choose from several icons to help you better recognize each of your tasks, but it doesn’t change what the task does.

Once created, the task doesn’t run all by itself. Rather, you decide when it runs by placing the corresponding Task Caller block somewhere in your main program. You can find it on the block palette, as shown above.

The simple tasks on this page work like so-called “My Blocks” as found in other apps. As you’ll see in the next sections, tasks can do a lot more than just combining multiple actions.

A closer look at program flow

To get some practice in making your own tasks, you’ll now combine everything you’ve learned so far. The next example sets up your drive base, makes the robot drive straight and turn, and repeats it all to drive in a square. You’ll create a task to drive in such a square so you can call it multiple times in your program.

This task makes the robot drive in a square.
This task makes the robot drive in a square.

Your robot should now drive in a square, pause for two seconds, and drive in another square.

Challenge #4.2.A: Tracing your steps ⸺ To get a better understanding of how this program runs all of its blocks, insert Print blocks as shown below. Before you try it, write down the expected output and verify your prediction. Discuss ⸺ Did you get it exactly right? If not, which steps did you miss?

Program for challenge #4.2.A. Placing Print blocks throughout your
program can help you verify that it works as intended. If not, it helps you
find out where it goes wrong. The result should be as follows.
Program for challenge #4.2.A. Placing Print blocks throughout your program can help you verify that it works as intended. If not, it helps you find out where it goes wrong. The result should be as follows.
Let's go!
---- Starting square ----
Straight
Turn
Straight
Turn
Straight
Turn
Straight
Turn
---- Completed a square ----
Let's go again!
---- Starting square ----
Straight
Turn
Straight
Turn
Straight
Turn
Straight
Turn
---- Completed a square ----

Using multiple tasks

You can create as many tasks as you need to organize your program. It does not matter where you place each task on the canvas. The following example program keeps the Square task and adds a task called Twinkle that plays a well-known melody.

You can make more than one task to organize your code.
You can make more than one task to organize your code.

A task may also call other tasks. In this example, the twinkle task calls the square task at the end. How many times do you hear the melody when you run this program? How many squares does the robot drive? Run the program to verify your prediction.

Challenge #4.2.B: Fast mode and slow mode ⸺ Create two new tasks called Slow and Fast, each with a collection of Drive Base Configuration blocks. The Slow task should set low acceleration and speed values for straights and turns. The Fast task should set high values. Now you can conveniently change speed profile midway throughout your program. Test it by driving one square very slowly and one square very quickly. Where do you place the Task Caller blocks?

A task must not call itself

A task should not call itself. To see why, let’s do a thought experiment. Imagine you’re putting up post-its for all of today’s tasks. You start doing your chores one by one. So far, so good.

But now imagine that one of your post-its said “put up another post-it with this text and act on it right away”. With no chance to take it down, you’ll keep putting up more and more post-its with the same text until you run out of paper or space on the wall!

How is this different from repeating actions forever? In a Repeat Forever block, actions complete before they repeat. In this case, you’ll be able to take your post-it off the wall since the task is complete. You’ll still be putting it back on the wall until the end of time, but at least you won’t run out of paper!

Challenge #4.2.C: An excursion into recursion ⸺ Modify the Square task to make it call itself as shown below. What do you think will happen? After many laps, you should see an error in the output pane. Research ⸺ Read the error message and determine which word to search for online. What is this technique called? Is it ever useful? Does this phenomenon occur anywhere in daily life?

Program for challenge #4.2.C. This task calls itself, which is
(almost) never a good idea.
Program for challenge #4.2.C. This task calls itself, which is (almost) never a good idea.