Search Header Logo
CS2 Quest 5: Functions

CS2 Quest 5: Functions

Assessment

Presentation

Computers

9th - 12th Grade

Practice Problem

Easy

Created by

Maria Cruz Farooqi

Used 4+ times

FREE Resource

1 Slide • 13 Questions

1

CS2 Quest 5: Functions

By Maria Cruz Farooqi

​Learning Target: I can write my own functions, use them in my code, explain why they make my programs easier to manage, and fix mistakes in function definitions.

media

2

Dropdown

Question image
A song is usually broken up into different parts, such as the
and verses. A chorus is a part of the song that stays the same and
throughout the song. If we were to have to type out the whole song, we would probably get annoyed because a lot of the song repeats itself. Wouldn’t it be nice if we could just jump back to another
and run that again?

3

Dropdown

Question image
Functions are really powerful programming constructs because they let us
that we have already written. This helps us manage the complexity of our programs because it can
that we need to write. It also helps make our programs easier to
and consequently maintain, because functionality is more clearly separated within our programs. We call this modularity and it is something that we should strive for within our programs if we want them to be easier to maintain,
upon, and change.

4

Drag and Drop

Question image
There are
we need to think about when writing our own functions. The first step is called “
” a function. It is where we write out the specific steps a computer needs to follow in order to successfully perform a specific action. The second step is “
” a function. This is where we tell our computer to
in the definition
Drag these tiles and drop them in the correct blank above
two steps
defining
calling
execute each step listed

5

Drag and Drop

Question image
When using functions in your program, you must first
your function. When you define a function, you must first start with a valid function header. The function header begins with the keyword “
”. This keyword is a cue for your computer to know that this block of code is a
. After def, you need to give your function a valid name (no spaces, don’t begin with numbers or special characters, etc). Right after the name, you need a set of
.
Drag these tiles and drop them in the correct blank above
define
def
parentheses and a colon
function definition

6

Dropdown

Question image
Once you have defined a function, you can
as many times as you would like. This tells your computer to
the function definition. It is important to know that you cannot call a function if you
. Once you call a function, then your computer will
.

7

Drag and Drop

Question image
All functions are given a unique
when they are defined. This allows the computer to find what
to execute. Take a look at the program and see where you see the word “chorus”. I see “chorus” on line 1, 5, 12, and 13. Now we know functions can be
, so which lines look like the same statement? 5, 12, and 13! This is an example of what it looks like to
a function. This leaves line 1 as an example of what it looks like to define a function.
Drag these tiles and drop them in the correct blank above
name
function you want
called many times
call

8

Multiple Choice

Question image

What is the name of the function?

1
execute
2

def

3
test
4
practice

9

Multiple Choice

Question image

On what line is the function called?

1
4
2
6
3
2
4
1

10

Multiple Choice

Question image

What is the result of calling the function?

1
Hello, world!
2
This is a test.
3
Function executed successfully.
4
Testing, testing. I'm working!

11

Match

Question image

Match the Key Terms

A block of organized, reusable code that is given a name.

The act of creating a new function. 

Python Syntax:

def name():

code


The act of executing a function definition.

Python Syntax:

name(arguments)


The spaces found at the beginning of a line of code. Necessary to do after a colon in Python!

Function

Define

Call

Indent

12

Multiple Choice

Question image

The following function is supposed to give your hero the power to patrol an area for enemies. If they find an enemy, your hero should attack. However, the function does not work as intended.

What is wrong? What would you do to fix this problem?


1

Change 'hero.attack(enemy)' to 'hero.defend( )'

2
Add a delay inside the 'while True:' loop
3
Remove the 'if enemy:' condition entirely
4

The function definition looks good. It just hasn’t been called yet. Call it in the while True loop.

13

Multiple Choice

Question image

Do these functions produce the same output if called? If yes, what is the output of both. If no, explain how they differ.


1

No, they do not produce the same output. Algorithm A looks for an item while algorithm B looks for an enemy.

2
No, they produce the same output but with different conditions.
3
Yes, they produce the same output: 'I'm tired'
4
Yes, they produce the same output: 'cleave'

14

Video Response

Record a thank you to a peer that helped you with coding.

video
Open Video Recorder

CS2 Quest 5: Functions

By Maria Cruz Farooqi

​Learning Target: I can write my own functions, use them in my code, explain why they make my programs easier to manage, and fix mistakes in function definitions.

media

Show answer

Auto Play

Slide 1 / 14

SLIDE