SHOP

PLAY

CREATE

3

Write your own functions

Min. Age: 6+ Mother and son; age icon Duration: 20 min. Mother and son; age icon

Video Example:

Material required: Download material .pdf

Whiteboard and marker
Whiteboard and marker
Function declaration and Function return blocks for coding
Function blocks
Code block
Code blocks
Function call tiles for drawing compound figures
Function call tiles for drawing compound figures
Function calls for drawing geometric shapes
Function calls for drawing geometric shapes
program BEGIN block
BEGIN block
program END block
END block

   Play        Write

1

What should my programs do?

Your programs should draw compositions of compound figures using the least possible code.

2

Why do functions exist?

Using the necessary blocks and what you know so far, imagine the code that can create this image:

Drawing with two girls a house an three birds on a tree
Programmer thinking
Not factored messy program

Does it seem laborious and repetitive to you? If you have reached this conclusion, you are on the right track! Can you think of a solution to avoid repeating the code several times when drawing the 2 girls and the 3 birds?

To solve this problem, functions were invented.

3

What are functions?

A function is a block of code that contains a series of instructions.

Each function performs a specific task such as "draw a bird" or "draw a girl". Every time you want to "draw a bird", instead of calling a bunch of functions to draw figures, simply call the "drawBird" function.

4

How do I create a function?

Function declaration block
Start with a function declaration block, in which you enter the name of the function.
Code block
Followed by the code blocks you need.
Function return block
End with a return block, which you can use in case you want to return a value when you call the function.

Declare and implement the 'drawBird' function as follows:

Draw bird function using block programming

That's it! Now you can call the 'drawBird' function whenever you want to draw a bird.

The 'drawBird' function in written code (do not show to very young players):

5

Now that I have my function, how do I use it?

1

Now you can reuse the function. Every time you call the 'drawBird' function, a bird is drawn.

Program that draws three birds, block-based programming and text-based programming
2

Using blocks, declare and implement the 'drawGirl' and 'drawHouse' functions.

3

You can now complete the following program to draw a tree with 3 birds and 2 girls next to a house using the 'drawHouse', 'drawBird', and 'drawGirl' functions.

Program that draws two girls, one house and three birds over a tree, block-based programming and text-based programming
6

Can I create my own functions and call them in my programs?

Of course! You already know how to create functions and how to reuse them in your programs! Create other programs that call your functions and draw other compositions. Run the programs and draw on the whiteboard.

You can use blank tiles to write new functions. Use the marker to write on them.

Three blank tiles and a marker

Use more tiles to draw other geometric shapes.

Function call tiles for drawing basic geometric shapes