Procedures¶
We don’t have to create the program as a whole.
It is far more clear if we divide pieces of code into smaller, separate parts - procedures. Defining procedures makes writing programs and finding mistakes easier. That is why it is good to keep the procedures in your “backpack”. That way, you will be able to use them in other programs you create, and not waste time on making them again.
Broadcasting¶
Programs made in Scratch usually have multiple sprites that interact with each other. We can define the interaction ourselves, and we can also use the built-in procedure That allows the sprites to broadcast messages and affect the running of the program. Using the broadcasting procedure will significantly decrease the number of blocks in the program, and it will make it easier for us to read the code.
- Program A
- When we run the program, we can see that all three girls are saying hello in their own language at the same time. This would not be the case in real life, during a normal conversation. We usually listen to the person we are talking to, and then we speak.
- Program B
- You are right! When we broadcast the messages, we allow everyone participating in the conversation to be polite - and not interrupt one another.
Q-7: There are two programs - A and B, presented in the figures above. Both programs contain 3 sprites, girls speaking English, German and French. Below each girl are blocks that belong to them (the blocks allow them to say hello to the other two in their own language). Create programs you see in the figures above. Analyze what happens when they run. Which program represents a simulation of a normal (regular) conversation between three strangers?
Watch the process of creating the program, which allows the girls to speak without interrupting one another:
Defining a new procedure¶
In Scratch, we can create a new procedure that would correspond to our needs.
Imagine that we want to create a program, which draws a flower on the stage. A flower is made of petals. Therefore, we need two procedures:
Petal, within which we will define the drawing of one petal, and
Flower, within which we will define the drawing of five petals.
In other words, the procedure Flower will call the procedure Petal, and the main program will call the procedure Flower.
We create a procedure by clicking on the category My Blocks, where we should click on the button Make a Block. A window will open, which will allow us to name our procedure.
If you want your flower to have the same petals like the one in the figure below, create the procedure Petal we presented:
We will let you make a procedure, which draws 5 petals. Little help: After drawing one petal, you need to turn the sprite 72 degrees to the right.
So, by using procedures, we make writing programs and finding mistakes easier.