CS2 - Slide Deck 5 - Functions
CS2 - Slide Deck 5 - Functions
Code Analysis:
● 4.A: Explain how a code segment or program functions.
● 4.B: Determine the result of code segments.
● 4.C Identify and correct errors in algorithms and programs including error discovery
through testing.
CodeCombat
Computer Science - 2
Quest #5
Functions
Let’s Review…
CODING CONCEPTS
1 What did we talk about last time?
COMPLETE QUESTS
4 Are there any quests you want to review?
Quest #5
By the end of my quest, I
will be able to…
💎 Define and call a
function.
💎 Describe how functions
help manage the
complexity of my
programs.
These songs
repeat a lot!
7
Functions
This function
makes the hero
A function is a block of sing the chorus!
reusable code that performs
a specific action.
The term function and
procedure are synonyms.
Python
The chorus
Using Functions function is
called 3 times!
def name( ):
The steps your computer
code
3
1
needs to execute.
Python
Calling Functions Syntax
name( )
1 21
chorus is
Spell the End with a called on
name exactly set of lines 5, 12,
as it appears parentheses & 13
in the
definition.
Python
Warm-Up:
1.
1 What is the name of
the function?
2.
2 On what line is the
function called?
3.
3 What is the result of
calling the function?
Python
Warm-Up: Solution
1.
1 The name is practice.
2.
2 Line 6.
3.
3 The hero says…
Testing, testing.
I’m working!
Python
To the Forest!
Starter Code 👀
Python
Reminder: Debugging!
You’re going to make mistakes. That’s a very NORMAL part of coding, even for
experts. Finding and fixing errors (debugging) is an important part of
programming. Consider using the following tools when debugging your
programs:
We are going to
● Error messages practice reading error
messages, using
● Comments comments, and
● hero.say() or print() hero.say in CS2!
● Inputs and outputs
● Tracing tools
● Debuggers
● Unit tests
Activity: Debugging
The following program is
intended to protect
villagers. However, it
does not work.
1.
1 Where is the error?
2.
2 How would you fix
this mistake?
Python
Independent Practice
Log into CodeCombat
https://codecombat.com/play/forest
Play Levels
CS2 Levels 23 - 26, CC Silent Village
Need Help?
Ask a friend or AI!
Key Terms
● Function: A block of organized, reusable code that
is given a name.
Python Syntax:
def name():
code
● Call: The act of executing a function definition.
Python Syntax:
name(arguments)
● Indent: The spaces found at the beginning of a line
of code. Necessary to do after a colon in Python!
Python
6
Any Questions? Quest #6
Functions with
Quest #5 Parameters
5
Functions
Quest #4 7 Quest #7
4
Nested Conditionals Events
3 Quest #3
Conditional Statements (Else)
8
2 Quest #2
Conditional Statements (If) CS2
Capstone +
1
Quest #1 Concept
Coordinate Systems Review
Wrap-Up 1
How do functions help us
manage the complexity
of our programs?
Python