Skip to main content
Skip to main content

Visual to text coding
Lesson 11: Adaptable functions

Years 5-6; 7-8

This is the eleventh in a series of lessons to transition from visual coding to text-based coding with a General Purpose Programming language.

Included videos can be used by a beginner teacher and/or students to see how to code each of the simple programs step-by-step in all three languages: Scratch, Python and JavaScript.

This lesson may take two to three 45-minute periods. It builds on the coding concept of functions (see ACTDIP030 in Digital Technologies Australian Curriculum, Years 7 and 8). With the addition of parameters, functions allow the programmer to adapt their reusable code’s behaviour, tapping into the Computational Thinking skills of generalisation, pattern recognition and abstraction


 


   
Next lesson    


Learning hook

Pig Latin is a language game where English words are altered to sound different, but using deceptively simple rules.

Here are some examples:

  • she eats too much duck becomes eshay eatsay ootay uchmay uckday
  • have three old bananas becomes avehay eethray olday ananasbay
  • his floor feels cold comes ishay oorflay eelsfay oldcay

For simplicity, we'll say there are three main rules for converting an English word into a Pig Latin word. Each rule has the following format:

  • If the word starts with [something], then [make changes].

As a class, study the examples above and try to figure out the three main rules for converting an English word into a Pig Latin word.


Using these three rules, you could create a function to make a Pig Latin word from any supplied English word, then display it. Complete the gaps in the function pseudocode below:

Function convertToPigLatin(englishWord)
  pigWord ← ''

  If englishWord starts with a consonant then
    pigWord ← all except the first letter from englishWord
    Append the first letter from englishWord to pigWord
  End If

  If englishWord starts with __________________ then
    ___________________________________________________
    ___________________________________________________
  End If

  If englishWord starts with __________________ then
    ___________________________________________________
  End If

  Append 'ay' to pigWord
  Display 'The Pig Latin word is ', pigWord

End Function

See Wikipedia for more details on the rules for Pig Latin.

Learning map and outcomes

In this lesson, students will:

  1. access an online programming environment for visual code (Scratch) and for General Purpose Programming (Python or JavaScript),
  2. learn the vocabulary of arguments and parameters,
  3. practice writing functions that include parameters,
  4. observe how functions reduce repetition in code,
  5. generate a size chart using a custom shape to visualise data.

Learning input

In the above video a simple function for drawing a square is improved by adding a parameter.

Let's do a vocabulary check:

  • Arguments are the values that are supplied when calling a function. Eg. 50, 100, 25
  • Parameters are variables in a function for holding the values that are supplied. Eg. side takes the values 50, 100 or 25, whatever is supplied when the call is made.
 

Learning construction

STEP 1: SETUP

For more on setting up, see Lesson 10. This lesson continues with a turtle graphics approach for each language.

STEP 2: WRITING A FUNCTION WITH ONE PARAMETER

The above video demonstrates writing a simple function with one parameter. Try it yourself!


Solution code:

STEP 3: WRITING A FUNCTION WITH TWO PARAMETERS

The above video adds a second parameter to the function. Try it yourself!


Solution code:

STEP 4: FUNCTION EXERCISE WITH TURTLE GRAPHICS

Carefully read the pseudocode below.

1   BEGIN
2     Function drawDiamond(sizeFactor)
3       Pen down
4       Left 60
5       Forward 30 * sizeFactor
6       Left 60
7       Forward 30 * sizeFactor
8       Left 120
9       Forward 30 * sizeFactor
10      Left 60
11      Forward 30 * sizeFactor
12      Left 60
13      Pen up
14    EndFunction
15
16    drawDiamond(1)
17    Forward 50
18    drawDiamond(1.5)
19    Forward 50
20    drawDiamond(0.5)
21    Forward 50
22  END

On paper, try to predict what the program will draw. The turtle starts facing right.

Share with another student to see if you predicted the same outcome.

Finally, implement the code in Scratch, as well as Python or JavaScript.



Solution code:

STEP 5: FUNCTION EXERCISE WITHOUT TURTLE GRAPHICS

Carefully read the pseudocode below. This program does not use turtle graphics.

1   BEGIN
2       Function displayWordRepeatedly(word, multiple)
3       i ← 1
4       While i <= multiple
5         Display i + ' ' + word
6         ii + 1
7       End While
8     EndFunction
9
10    displayWordRepeatedly('banana', 5)
11    displayWordRepeatedly('apple', 3)
12  END

Predict the output of the program.

Now, implement the code in Python or JavaScript:


Solution code:

STEP 6: REDUCING REPETITIVE CODE WITH FUNCTIONS

The above video demonstrates the power of functions to reduce repetitive code, and ends with a challenge. The video shows Python, but it can be applied the same in JavaScript.


Solution code:


The above video concludes with a sample solution.

Solution code:

Challenge

These challenges use the skills covered so far.

  1. Write and test a function to create a car shape as below (must be all straight lines).

    Image of a car

    Now, modify your function to have a sizeFactor parameter, as with the diamond in step 4 of this lesson. Test it by calling it from the main program with arguments 1, 2 and 0.5.

    Finally, call your finished function with specific values so as to generate a size chart of 2018 new car sales data for four countries - China, USA, UK and Australia. eg. China's sales were about 23.7 million, so use a size factor of 2.37. USA size factor would be 0.53, and so on.

    Optional discussion question:

    Do you think your function allows your chart to give a fair representation of the difference in car sales between these four countries? How could you adjust it to more accurately reflect the differences?


Solution code:

  1. Write and test a function to generate and display the sequence of Hailstone numbers for any given starting number. The sequence should continue until you get to the number 1, or the sequence reaches a thousand numbers in length, whichever comes first.

    Hailstone numbers are generated as follows:

    • Start with a given first number.
      • If the first number is even, divide it by two to get the next number.
      • Otherwise (odd), triple the first number and add 1 to get the next number.
    • Now repeat the above with the next number to get the following one, and so on.

    eg. Starting with 12, we get the sequence: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1. This sequence reaches 1 with only ten numbers.

    Begin by writing pseuocode for the function and a simple test. Your function will need a loop inside.

    Now write the code in Python or JavaScript.


Solution code:

  1. (More challenging) Write a function to convert an English word to Pig Latin and display it, using the rules in the Learning Hook section of this lesson.

    Whether you code in Python or JavaScript, you will need to investigate commands for string manipulation, such as identifying characters that appear at the beginning of text. Note: Scratch does not have the required string manipulation capabilities. (This challenge can be done using spreadsheet functions in Microsoft Excel or Google Sheets.)

Solution code:

Resources

  • Setting up online environments
  • Online environments for coding in each language
    • Scratch
    • repl.it: an online environment suited to Python
    • JSFiddle: an online environment suited to JavaScript
  • Cheat sheets listing basic commands for coding:
    • Python Cheatsheet (from Grok Learning)
    • JavaScript CheatSheet (Tip: Press the little blue tabs to move Variables, Basics, Strings and Data Types to the top.)