Lesson 10: Simple functions
About this lesson
This is the tenth in a series of lessons to transition from visual coding to text-based coding with a general-purpose programming language.
Year band: 7-8, 5-6Curriculum Links Assessment
Links with Digital Technologies Curriculum Area
|Processes and Production Skills||5-6||
Design, modify and follow simple algorithms involving sequences of steps, branching, and iteration (repetition) (ACTDIP019)
Design algorithms represented diagrammatically and in English, and trace algorithms to predict output for a given input and to identify errors (ACTDIP029)
Implement and modify programs with user interfaces involving branching, iteration and functions in general-purpose programming language (ACTDIP030)
Students undertake a self-reflection of the programming task. The teacher can use the completed self-assessments to assist in summative assessment.
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:
- Instead of just thinking about the sequence of statements in a program, you can now organise your code in terms of the actions your program does.
- Functions can make your code more readable, especially if they are named well. It’s easier to find the bits of the code you want to improve or fix, and your main program is smaller and cleaner.
- It’s easier for multiple programmers to work on the same project. One person can concentrate on the code inside a function while another works on the main program.
- Functions can be written to adapt their behaviour according to values provided. We’ll learn how in Lesson 11.
Image credit: D Mitriy/ Wikimedia Commons
Learning map and outcomes
In this lesson, students will:
- code functions to draw shapes, then reuse them to make fun patterns,
- use functions to draw a cosmic scene full of stars.
What's with the turtle?
Turtle graphics has been around since the original Logo language for teaching programming to students, developed in 1966-1967. It is being introduced at this point in this course because it is an effective context to learn about functions, but examples will also be provided for writing functions outside of the turtle graphics context.
Like today’s Bee-bots and other rovers used in classrooms, the ‘turtle’ can be thought of as a robot moving about on the floor, and capable of drawing lines as it moves. Programmers issue simple instructions like forward 20 to move forward 20 units of distance, or right 90 to rotate clockwise 90 degrees.
To activate the pen, the typical command is pen down (think of a pen being dragged along the floor by the robot). The command pen up lifts the pen off the floor, allowing the robot to move around without drawing.
In Scratch, sprites are already moved around using the blue motion blocks. Any sprite can become a drawing turtle by using the Pen extension, which brings in the pen down and pen up commands.
- No need to create functions yet.
- Try to make your letters as accurate as possible, with the same height and appropriate width.
- It is easier at first to do straight lines than curves, so consider doing letters that are “blocky”.
Step 1: Function challenge in Scratch
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.
Step 3: Function exercise with turtle graphics
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.
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.
STEP 4: Function exercise without turtle graphics
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.
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.
- Setting up online environments
- Online environments for coding in each language
- Cheat sheets listing basic commands for coding: