How to use the
Learning Portal

Learning Portal

Our hands-on, comprehensive lesson plans span a range of levels. Browse our free STEM and coding learning resources.

Marty Image

3.03: Introducing Functions

60 Minutes

Lesson Overview

In this lesson, we will introduce functions. Functions are an important part of programming that reduce the amount of repeated code by being responsible for ideally one thing.

Learners will begin by thinking about games they may have played when they were younger and create a tactic which is akin to a function in a program. Later, learners will either repurpose old code by organising repeating blocks in a function, or develop the parts for functions that are already available in MartyBlocks.

Key vocabulary:
    function, readability, debugging,

Content Sections

  • Learning Objectives
  • Warm up
      • Get learning
      • Time for practice
      • Cool Down
        • Extensions & Challenges
        • Extend
          • Support
            • Additional Reading
            • 3.03: Introducing Functions

              60 Minutes

              Lesson Overview

              In this lesson, we will introduce functions. Functions are an important part of programming that reduce the amount of repeated code by being responsible for ideally one thing.

              Learners will begin by thinking about games they may have played when they were younger and create a tactic which is akin to a function in a program. Later, learners will either repurpose old code by organising repeating blocks in a function, or develop the parts for functions that are already available in MartyBlocks.

              Key vocabulary:
                function, readability, debugging,
              • Experience using sensors and variables
              • Tablet with Bluetooth 4.2+
                • Marty the Robot V2
                • Marty Workbook
                • Tablets
                • Access to the MartyBlocks editor

              Learning Objectives

              • I can define the purpose of a function.
              • I can use functions to make a program easier to read and understand.

              Warm up

              Have learners think of a playground games they may have played when they were younger. Now, have them think if there were any that they regularly won (because they figured out something) or that they often saw others win at, which was frustrating. Ask if learners ever thought this was like cheating. Was there something that the winning players always seemed to do which led to a win? Below are some ideas:

              • Simon Says
              • Duck, Duck, Goose
              • Capture the Flag
              • Dodgeball
              • Three-legged Race
              • Hide and Seek
              • Four Square

              Here are talking points for Duck, Duck, Goose or look at slide 4 in the presentation, which includes a video

              • If I were sitting, I would be watching the person going around the circle,
              • as they approached me, I would prepare myself to get up - hands on the ground ready to push,
              • legs not cross and relaxed but feet planted and knees bent, ready to push
              • if I were walking around the circle, I would be watching for people talking
              • I would avoid people watching me
              • I would prepare to move quickly before touching a person

              Inform learners that this game has two main functions for winning, one function is for the person walking around the circle, one function is for people sitting in the circle. These are two very different parts of the game because they are for distinct roles in the game. In programming, we separate these parts by creating functions. In this case, we could have the program duckDuckGoose and one function could be sitting and the other could be walking.

              Slide 5 from the presentation has a suggested flowchart that could be used to illustrate a function for Duck, Duck, Goose. Learners will need access to the internet to research the old-school game they have chosen to write about. The minimum they will need to research is if there is a point system, if there are teams and what decides a winner.

              Get learning

              Learners will need to take one of the games researched and decide on a tactic they think would be most beneficial for winning. The group will need time to discuss the ideas in order to reach consensus.

              Once they have decided, learners will use pseudocode to describe the routine they are going to use and give it a name. Then, they will need to place it in the ‘program’ of the game. Groups will present their ideas to the class describing how their tactic can be used repeatedly through the game to improve the chances of success.

              Show slide 7 on the presentation to display the function block, in Martyblocks, the section is called My Blocks. There are notes on slide 7 to support discussion.

              Following the idea sharing, display Marty standing with legs side by side, and press the walk forward block on your device (alternatively, use slide 8 from the presentation, which shows a slowly walking Marty. There are notes on the page that describe how you can use this slide). This will cause Marty to take two steps forward, by default. Have learners look carefully as you press the button again. What different movement do you see Marty make to carry out that command?

              Have learners take their group Marty and access the Marty controller. This will allow learners to repeatedly see the walk action that Marty performs. Additionally, learners can use the speed slider to slow down Marty to see the specific movements they make. As a group, challenge learners to create a function for Marty to walk. This needn't be recorded in the workbook, it is more a chance to get a feel for how the individual commands come together to make a function.

              There is a description of the blocks used to have Marty walk in the teacher guide and slide 9 shows the blocks for learners to try this after they have had a chance to experiment. Also, take time to go over the different ideas learners had about the movements.

              Time for practice

              Suggest that learners investigate the kick block. Have learners repeatedly watch the movements of the block and note down the steps they see. This will take some time because of the complexity and speed of some of these blocks. Remind learners of the need for timing movements properly and for the need to sometimes run different commands in parallel.

              Slide 10 shows the kick movement. The teacher guide has the code that is needed to carry out this action. You can choose to either show the video, build the code in MartyBlocks or just tap the kick block on the Marty Controller or in MartyBlocks. Take time to walk around listening to discussion and asking questions to promote thinking about the sequence and timing. Every so often, add a question about why we use fuctions.

              If space allows, encourage learners to watch ideas from other groups, asking questions about why they chose to put certain code where they did.

              Give time before the cool down to allow learners to showcase their functions. Ask them if they would be as likely to program display Marty dancing if they had to create the function each time: reusable functions save time!

              Cool Down

              Bring learners back together to discuss the challenges they faced and overcame. Have groups model their creations and explain what is happening, when. Encourage other groups to ask questions to deepen the understanding of the processes.

              Suggested questions you might ask:

              • Would you expect to use a function every time you sit down to code? Why or why not?
              • How many reasons can you think of to consider using functions in code?
              • If you encountered a bug in your code, do you think it would be quicker to find and fix if you had or didn't have functions? Why do you think this?

              Carry out any end of lesson routines.

              Log off devices and clear everything away.

              Extensions & Support

              Extend

              In the example video, Marty's walking speed is reliant on the level of light. There is fast and there is slow.

              • challenge learners to transition between the two: if Marty was walking slowly and then walks more quickly; learners will need to store a variable based on the last walking speed.

              Support

              Have physical code cards available for learners to use, when building their scripts:

              • if/then and if/then/else blocks
              • loops
              • light sensor blocks
              • operator blocks: <, >, =
              • a variety of movement blocks

              Have a print out of the basic function statements, using the event block and 'light_action' block.

              Additional Reading

              What are the My Blocks


              • Middle School Technology Applications: Grade 6 to Grade 8
              • Technologies: Computing Science
              • Literacy & English: Listening and Talking
              • Health and Wellbeing: Mental, Emotional, Social and Physical Wellbeing
              • Literacy & English: Writing
              • Computing, Design and Technology: Design and Technology
              • Computing, Design and Technology: Computing
              • CSTA Education Standards
              • Digital Technologies, Design & technologies: Design & Technologies
              • Digital Technologies, Design & technologies: Digital Technologies
              • International Society for Technology in Education (ISTE)