1 of 16

Lesson 9: Walk the distanceTo

Challenge focus: 41 - 45

Coding Concepts: Returning value from functions and methods

Coding Adventure Part I

Key stage II

Class IV

2 of 16

Lesson Overview

Challenge Range

Coding Concepts

Sub Concepts

Resources

41 - 45

Variables

  • Returning value from functions and methods
  • Use variables to hold values returned from function (distanceTo).

Lesson Plan 30

Walk the distanceTo

Coding Adventure Part I

Key stage II

Class IV

3 of 16

Lesson Objectives

  • Understand that functions can perform tasks and provide results, known as return values.
  • Learn how variables are utilized to store and manage values returned by functions.
  • Learn the syntax of assigning a variable to store the result of a function.
  • To supply values to functions using variable.
  • To supply returned values directly to function.
  • Complete challenges 41 - 45.

Coding Adventure Part I

Key stage II

Class IV

4 of 16

Returned values from a function

  • Functions often yield values upon completion of their tasks.
  • Typically, these returned values are captured in a variable for subsequent use in the code.

Coding Adventure Part I

Key stage II

Class IV

5 of 16

Passing down values using variable

Working of the code:

  • Step 1: turtle.distanceTo bush This code will execute and return distance of turtle from the bush.
  • Step 2: The returned distance from the turtle.distanceTo bush code will be assigned to d.
  • Step 3: The value inside variable d will be passed to turtle.step.
  • Step 4: turtle.step d will get executed.

1

d = turtle.distanceTo bush

2

turtle.step d

Coding Adventure Part I

Key stage II

Class IV

6 of 16

Passing down values without using variable

turtle.step turtle.distanceTo bush

Working of the code:

Step 1: turtle.distanceTo bush This code will execute and return distance of turtle from the bush.

Step 2: The returned distance from the turtle.distanceTo bush supplied to turtle.step

Coding Adventure Part I

Key stage II

Class IV

7 of 16

Demo 1

Using challenge 41, demonstrate the use of returned values to solve the problem in 5 lines.

  1. Pass an appropriate variable to a method.
  2. Add a line of code that calls a function of turtle, which accepts values from another function.

Coding Adventure Part I

Key stage II

Class IV

8 of 16

Demo 1 Solution:

Coding Adventure Part I

Key stage II

Class IV

9 of 16

Activity 1: Debugging

Complete the code for challenge 42 so that monkey can reach banana on the other side of the river

  • Explain the given code.�step distanceTo banana
  • Write an algorithm to solve the problem.
  • Use the above algorithm and write a code to collect the banana.

Coding Adventure Part I

Key stage II

Class IV

10 of 16

Activity 1 Solution

i) Breaking down code

Step 1: distanceTo function returns the distance from the monkey to the banana.

Step 2: This returned value is given to step function

ii) Algorithm to solve the problem

Step 1: Start

Step 2: Turn monkey to banana

Step 3: Calculate the distance between banana and monkey

Step 3: Walk monkey to banana

Step 4: Stop

Coding Adventure Part I

Key stage II

Class IV

11 of 16

Activity 1 Solution

iii)

Coding Adventure Part I

Key stage II

Class IV

12 of 16

Activity 2

  • Log into your CodeMonkey account.
  • Open Coding Adventure Part 1
  • Complete challenges 43 to 45. Challenge 41 & 42 have already been demonstrated to you earlier.

Coding Adventure Part I

Key stage II

Class IV

13 of 16

Activity 2 Solution

43

Coding Adventure Part I

Key stage II

Class IV

14 of 16

Activity 2 Solution

44

Coding Adventure Part I

Key stage II

Class IV

15 of 16

Activity 2 Solution

45

Coding Adventure Part I

Key stage II

Class IV

16 of 16

Key Points

  • Functions in programming can execute tasks and produce results, which are known as return values.
  • Variables play a crucial role in storing and managing the values returned by functions.
  • Learn the syntax of assigning a variable to capture and store the result of a function.
  • Passing values to functions using variables, enhancing flexibility.
  • Directly passing returned values from one function as inputs to another function.

Coding Adventure Part I

Key stage II

Class IV