CS2 - Slide Deck 6 - Functions With Parameters
CS2 - Slide Deck 6 - Functions With Parameters
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 #6
Functions with Parameters
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 #6
By the end of my quest, I
will be able to…
💎 Define and call a function
with parameters.
💎 Describe how functions
help manage the
complexity of my
programs.
The song
repeats a
lot!
7
Functions with Parameters
Parameters represent This function
makes the hero
information that we can sing part of a
verse!
pass to a function
definition that modifies its
behavior.
Python
Passing Arguments
You can change
the value you pass
to a function.
Separate each
argument passed The verse
with a comma. function is
called 2 times!
Python
Arguments vs. Parameters
An argument is the
A parameter is a
value that is passed
variable you define
to the function
in your function.
when it's called.
Procedural Abstraction
Procedural abstraction allows
us to generalize our programs so
that they work with a variety of
inputs.
Pros:
● Reduces lines of code
● Easier to read and comprehend
Image Source
Python
Warm-Up Part 1: Solution
1.
1 sayHello
1.
2 “Amara”,
“15”,“Miss
Hushbaum”,
“32”
2.
3 name, age
Python
Warm-Up Part 2:
Write the program your
hero would need in order I smell at least
to sing this version of Old 2 function
definitions!
MacDonald.
Remember to prioritize
procedural abstraction.
Fewest lines of code wins!
Warm-Up Part 2: Solution
Answers may vary.
Can you beat
21 lines of
code?
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 safely get inside the
door. 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 27-36, Concept Challenges: Timberland
Trap, Last Cannon, and Functional Patrol
Need Help?
Ask a friend or AI!
Key Terms
● Parameter: Special variables that are used to
represent the variety of information that can be
given to a function.
Python Syntax:
def name(parameters):
code that uses parameters
● Argument: Specific values that are passed to a
function call.
Python Syntax:
name(arguments)
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 with
parameters help us
manage the complexity
of our programs?
Hint: Think about the
pros of procedural
abstraction.
Wrap-Up 2
The following program is supposed to make your hero sing Happy
Birthday to Okar. However, the program does not work as intended.