Lesson 15: Functions in Minecraft

Overview

Students will begin to understand how functions can be helpful in this fun and interactive Minecraft adventure!

Purpose

Students will discover the versatility of programming by practicing functions in different environments. Here, students will recognize reusable patterns and be able to incorporate named blocks to call pre-defined functions.

Agenda

Warm Up (10 min)

Bridging Activity - Functions (15 min)

Main Activity (30 min)

Wrap Up (15 min)

View on Code Studio

Objectives

Students will be able to:

  • Use functions to simplify complex programs.
  • Use pre-determined functions to complete commonly repeated tasks.

Preparation

  • Play through the puzzles to find any potential problem areas for your class.
  • Make sure every student has a journal.

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Students

Vocabulary

  • Function - A piece of code that you can easily call over and over again.

Support

Report a Bug

Teaching Guide

Warm Up (10 min)

Introduction

Help the class understand that functions are simply a chunk of code that has a name. Once defined, you can use that name over and over in your program to tell the computer to run the chunk of code that you assigned to it.

Bridging Activity - Functions (15 min)

This activity will help bring the unplugged concepts from "Functions Unplugged: Songwriting" into the online world that the students are moving into. Choose one of the following to do with your class:

Lesson Tip

Function blocks:

The block to the left is a function declaration, a block that students will name and use to fill in the function. The block to the right is a function call, a block that makes the function code run. Students will need multiple of the function call blocks.

Unplugged Activity Using Some Blockly

Pick a song to play that the students enjoy and print out the lyrics. You can use the same song from "Functions Unplugged: Songwriting." Break your class into groups or pairs. Pass out the printed out lyrics (including the repeated chorus) and the basic function blocks from Unplugged Blocks (Courses C-F) - Manipulatives to each group or pair of students. See lesson tip for details.

Ask the students to cross out any part of the song that can be made into a function (the chorus is a good example) and put it into the function blocks provided. Students should fill in the function declaration with a function name and the words of the repeated lyrics. Once the function declaration is done, ask the students to fill in the function calls and place them on top of the crossed out lyrics.

Once every group or pair is done, ask the class where they put their functions and why. Did everyone make the same function? How often is the function repeated?

Preview of Online Puzzles

Pull up a puzzle from the lesson. We recommend puzzle 9. As a class, work through the puzzle without using functions. Once you have gotten the solution, display it on a white board or overhead. Ask the class to point to the repeated code. Ask the class how they would simplify the program. Why can you not just use a loop?

On the white board or overhead, rewrite the program without the repeated code, but leaving one line space. In that/those line space(s), call a function. Off to the side, declare the function like the left example block in the lesson tip. Ask the class what they think the code will do now.

Open up a discussion with the class on why functions could be useful in programming. Invite students to discuss the difference between functions and loops.

Main Activity (30 min)

Online Puzzles

We recommend providing paper and pencils for students to write (or draw) out ideas. Also, if students are having trouble recognizing patterns, have them work with a partner on the harder puzzles.

Wrap Up (15 min)

Journaling

Having students write about what they learned, why it’s useful, and how they feel about it can help solidify any knowledge they obtained today and build a review sheet for them to look to in the future.

  • What was today's lesson about?
  • How do you feel about today's lesson?
  • What did your functions do in the programs you wrote today? How did that help you?
  • When should you use a function instead of a loop?
View on Code Studio

Student Instructions

The door is locked, but the Agent is here to help!

Snap a move forward block to the bottom of the when run block in the workspace to get the Agent to the pressure plate [PressurePlate_Up], then press "Run" and use the arrow keys to move out of the house to collect the chest [chest].

View on Code Studio

Student Instructions

{currentPlayerName} is headed on an adventure! To help, get the map [map_empty] behind the locked door on the right.

Snap the code into the workspace to move the Agent to the pressure plate [PressurePlate_Up] so that {currentPlayerName} can get through.

View on Code Studio

Student Instructions

{currentPlayerName} continues on the adventure and needs the compass [compass] to prepare for the trip.

Code the Agent to open the gate.

View on Code Studio

Student Instructions

The Agent can move across water!

Use a repeat loop to solve this puzzle and help {currentPlayerName} get to the bucket [bucket_empty] behind the door.

View on Code Studio

Student Instructions

You made it across!

Now, get {currentPlayerName} safely through the maze to collect the axe [diamond_axe]

View on Code Studio

Student Instructions

Oh no, the bridge is out!

Code the Agent to rebuild the bridge using the place block so that {currentPlayerName} can get to the shovel [diamond_shovel].

View on Code Studio

Student Instructions

Another river, but this one is much wider!

Fix this code to help the Agent build a bridge so that {currentPlayerName} can get to the pickaxe [diamond_pickaxe].

View on Code Studio

Student Instructions

A function named build bridge is in your workspace. All of the code you need to cross the river is built right in!

Snap the small, green build bridge function block into your workspace twice to fix the path and get {currentPlayerName} to the Redstone Torch [redstone_torch].

View on Code Studio

Student Instructions

There's ice in the way!

The clear path function can help you destroy the ice and get {currentPlayerName} to the Minecart [minecart_normal]!

View on Code Studio

Student Instructions

Can you spot the difference between the two functions in your workspace?

Use these functions with other code from the toolbox to clear a path for the Minecart, then collect the Flint and Steel [flint_and_steel].

View on Code Studio

Student Instructions

Look at the functions below. How can you use them to avoid the Ghasts and help {currentPlayerName} make it safely to the Enchanted Book [book_enchanted]?

  • Free Play
  • 12
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Freeplay: Now it's up to you! Use the Agent's code to help you explore this world.

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 1B-AP-08 - Compare and refine multiple algorithms for the same task and determine which is the most appropriate.
  • 1B-AP-11 - Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.