Imagine you’ve been put in charge of a colony ship so that civilisation can be built on a new planet. Does this task seem daunting?
As a class, see if you can list some of the tasks that will need to be done once the ship lands on the new world.
When projects are large, it’s usually necessary to delegate and rely on different people with different experience, talent and skills. Did you know that, according to Wikipedia’s statistics in February 2020, approximately 140 000 people performed an action or contributed to Wikipedia in the previous 30 days?
As computer programs get larger, it becomes impractical to have all the different tasks in one large program. Instead, we separate the code for specific tasks into smaller functions (sometimes called subroutines).
The code left behind is now called the main program (or main routine). From there we start the program, activate (or call) the functions we want to use and then end the program.
This has several advantages:
In this lesson, students will:
The above video demonstrates a basic shape-drawing function in Scratch and ends with a challenge. Try it yourself before checking the conclusion of the video below.
Carefully read the pseudocode below.
1 BEGIN 2 Function drawHouse() 3 Forward 50 4 Left 90 5 Forward 50 6 Left 45 7 Forward 35 8 Left 90 9 Forward 35 10 Left 45 11 Forward 50 12 Left 90 13 EndFunction 14 15 Pen down 16 drawHouse() 17 Pen up 18 Forward 100 19 Pen down 20 drawHouse() 21 END
This program draws not one but _______ houses side by side. The main program begins at line number ____. After activating the pen, the program calls the ______________ function, which produces a shape. The function begins at line number ____ and ends at line number ____. Once the house shape is drawn, we return to the _______ __________. Next, the pen is ______________ and the turtle moves forward 100 units. Finally, the pen is reactivated and ______________ is called again.
Click for solution.
This program draws not one but two houses side by side. The main program begins at line number 15. After activating the pen, the program calls the drawHouse function, which produces a shape. The function begins at line number 2 and ends at line number 13. Once the house shape is drawn, we return to the main program. Next, the pen is deactivated and the turtle moves forward 100 units. Finally, the pen is reactivated and drawHouse is called again.
Carefully read the pseudocode below. This program does not use turtle graphics.
1 BEGIN 2 Function assignPasscode() 3 answer ← Input 'What is your name?' 4 Display 'Hi', answer, '. Here is your passcode:' 5 passcode ← Random number between 1000 and 9999 6 Display passcode 7 EndFunction 8 9 Repeat 4 times 10 Display 'Assigning a new passcode...' 11 assignPasscode() 12 Display '' 13 End Repeat 14 END
Now, fill in the missing words in the paragraph below, to explain what the program does.
This program assigns random passcodes for four people. The main program begins at line number ___. It contains a _______ which happens ___ times. Each time, it prints ‘Assigning a new passcode...’ and also ________ a function called assignPasscode(). The function asks the user for their ________. It then generates a passcode as a number between _______ and _______. This is displayed for the user.
Click for solution.
This program assigns random passcodes for four people. The main program begins at line number 9. It contains a loop which happens 4 times. Each time, it prints ‘Assigning a new passcode...’ and also calls a function called assignPasscode(). The function asks the user for their name. It then generates a passcode as a number between 1000 and 9999. This is displayed for the user.
These challenges use the skills covered so far.
Write and test a function to create a small star shape. In the same program, write and test another to create a big star shape. See the images below for sample shapes to aim for.
With your functions created, use the main program to create a star-filled sky with a mix of large and small stars.
Note: You may choose to use loops in the main program to help reduce repetition.
Write and test a function to create a small geometric shape. Use that function as part of a main program to create a geometric design. As a further challenge add colour to the design. Here’s an example.
(Without turtle graphics) Choose a popular song with a few verses as well as a chorus that recurs. Write and test one function for each verse, as well as one function for the chorus. These functions should simply display the words.
In the main program, call the verse and chorus functions in the correct order to display the complete song. You may need to call the chorus function more than once.