Skip to content
Binogi logo

Subjects

Functions (programming)

Functions (programming)

Video thumbnail
Play

True or false? A function is like a small program inside our big program.

Functions (programming)

Lina has programmed several short programs for her robot. Now she wants to put together two of these into one larger program. Lina's bundled pseudocode from the two programs looks like this: There are very many rows of instructions. Lina scrolls up and down through the code... She finds three rows of instructions that are repeated several times.

Is that necessary? Let’s help her, so that the instructions are only needed in one place. These rows with three instructions exist in two places. Let’s write the same three instructions at the top of the pseudocode. We are about to create a small program within our big program, a function.

We use a function by calling it. Then the function needs a name so that we know we’re calling the correct function. We name it “start”. And then we put two parentheses after the name. The parentheses are to show that it is a function.

We want to make the pseudocode clearer. We do this by indenting the rows related to the function. Now we can call the start-function… Here... and here, by typing "call start" and two parentheses. When using functions, we don’t need to write the same instructions many times.

The pseudocode becomes easier to use because each call of a function performs several instructions. It will also be easier to debug a short program than a long one with lots of rows. And if we want to make a change, we only need to do it in one place. We have replaced two groups of code with two function calls. Now Lina is eager to convert more parts of her code into functions.

Can you see any more groups of instruction-rows that are repeated? Here are three other instructions that exist in two places. Maybe you can help her?