Lesson 28: Functions in Bee
Overview
This lesson teaches students how to create simple functions using our sophisticated “modal” function editor, preparing the way for them to incorporate parameters in future lessons.
Purpose
Students will discover the versatility of programming by practicing functions in different environments. Here, students will recognize patterns in the bee's maze. The bee will need to navigate the play area, collect nectar, and make honey. Students will learn to organize their programs and create functions for repeated code.
Agenda
Warm Up (15 min)
Bridging Activity - Functions (15 min)
Main Activity (30 min)
Wrap Up (15 min)
Extended Learning
View on Code Studio
Objectives
Students will be able to:
- Categorize and generalize code into useful functions.
- Recognize when a function could help to simplify a program.
Preparation
- Play through the CSF Express Course - Website associated with this lesson to find any potential problem areas for your class.
- Review CS Fundamentals Main Activity Tips - Lesson Recommendations.
- Make sure every student has a Think Spot Journal - Reflection Journal.
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the Teachers
- CSF Express Course - Website
- CS Fundamentals Main Activity Tips - Lesson Recommendations
For the Students
- Unplugged Blocks (Courses C-F) - Manipulatives
- Think Spot Journal - Reflection Journal
Vocabulary
- Function - A piece of code that you can easily call over and over again.
- Parameter - An extra piece of information passed to a function to customize it for a specific need
Support
Report a Bug
Teaching Guide
Warm Up (15 min)
Introduction
Students that have done Course E Online Puzzles - Website will already have experience using functions to solve online puzzles, but the function editor in this course is slightly different. Let students know that they will get a quick review of simple functions before moving in to more difficult challenges with the new "modal" editor.
For the students who are less familiar with using functions online, start by reviewing the vocabulary words from the "Functions Unplugged: Songwriting with Parameters".
- Function - Say it with me: Func-shun
A piece of code that you can call over and over again.
- Parameter - Say it with me: Pa-ram-eh-ter
An extra piece of information that you pass to the function to customize it for a specific need.
Tell the class that there are two main components to using functions with parameters.
-
The Declaration: Function declarations are what create a function. In a function declaration, you fill in the function with code and you give the function a name. Inside the function declaration you should note where the parameter is used inside the function code. You must declare a function before you can use it.
-
The Call: Function calls are what makes the program run the code in the function. To call a function, you place the name of the function in your program with a value for the parameter. Make sure your function is properly defined (with a parameter) before calling it in your program.
The class can use songwriting as an example to understand these two components. In the unplugged activity, the function containing the lyrics to the chorus was named "chorus". When we first made this function, we circled the lyrics that would go in the function. Once we named the function, we could read through the lyrics and replace the repeated chorus lyrics with a function call to "chorus".
Continue the conversation until students have a basic understanding of functions being declared and called. If students don't get to this point, make sure to do one of the bridging activities before moving into the Code.org puzzles.
Note: Students will not be using parameters in their functions today. However, it's good to review what parameters are and why they are used for next time.
Bridging Activity - Functions (15 min)
This activity will help bring the unplugged concepts from “Functions Unplugged: Songwriting with Parameters” 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 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 Paper Blocks
Pick a song to play that the students enjoy and print out the lyrics. You can use the same song from "Functions Unplugged: Songwriting With Parameters." Break your class into groups or pairs. Pass out the printed out lyrics (including the repeated chorus) and the function with parameter 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, even if it has a couple of different words (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?
Previewing Online Puzzles as a Class
Pull up a puzzle from Course F Online Puzzles - 2018 - Website. We recommend the 12th puzzle for this activity. 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.
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)
CSF Express Course - Website
Students may benefit from writing code without functions then create functions from the repeated code. If students don't enjoy doing this in the Code.org workspace, we recommend providing paper and pencils for students to write (or draw) out their ideas.
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.
Journal Prompts:
- What was today’s lesson about?
- How did you feel during today’s lesson?
- How did functions help the bee collect nectar and make honey more efficiently?
- Can you imagine using parameters in these puzzles? If so, explain how. If not, why not?
Extended Learning
Draw by Functions
Break the class into groups of 2-3 students. Have each group write a function that draws some kind of shape and a program that uses that function. Depending on the creativity or focus the groups, students might need to be assigned a shape to create. Once every group is done, have the groups switch programs. On a separate piece of paper, each group should draw what the program creates. The groups should then return the programs and drawings to the original group.
Did every group get the drawing they expected? If not, what went wrong? Have the class go through the debugging process and try again.
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-09 - Create programs that use variables to store and modify data.
- 1B-AP-11 - Decompose (break down) problems into smaller, manageable subproblems to facilitate the program development process.