[go: up one dir, main page]

0% found this document useful (0 votes)
11 views24 pages

CS2 - Slide Deck 5 - Functions

The document outlines key concepts in algorithms and programming, emphasizing the importance of abstraction and algorithmic thinking in program development. It introduces computational thinking practices, including solution design, code analysis, and debugging techniques, particularly focusing on functions and their role in managing program complexity. Additionally, it provides exercises and activities for practical application of these concepts in coding environments like CodeCombat.

Uploaded by

SOE LIN HTET
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views24 pages

CS2 - Slide Deck 5 - Functions

The document outlines key concepts in algorithms and programming, emphasizing the importance of abstraction and algorithmic thinking in program development. It introduces computational thinking practices, including solution design, code analysis, and debugging techniques, particularly focusing on functions and their role in managing program complexity. Additionally, it provides exercises and activities for practical application of these concepts in coding environments like CodeCombat.

Uploaded by

SOE LIN HTET
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Today’s Big Ideas

Algorithms and Programming (AAP):


Programmers integrate algorithms and abstraction to create programs for
creative purposes and to solve problems. Using multiple program statements in a
specified order, making decisions, and repeating the same process multiple times
are the building blocks of programs. Incorporating elements of abstraction, by
breaking problems down into interacting pieces, each with their own purpose,
makes writing complex programs easier. Programmers need to think
algorithmically and use abstraction to define and interpret processes that are used
in a program.
Today’s Computational Thinking
Practices
Computational Solution Design:
● 1.A: Investigate a situation, context, or task.
● 1.B: Determine and design an appropriate method or approach to achieve the purpose.
● 1.C: Explain how collaboration affects the development of a solution.
● 1.D Evaluate solution options.

Algorithms and Program Development:


● 2.A: Represent algorithmic processes without using a programming language.
● 2.B: Implement and apply an algorithm.
Today’s Computational Thinking
Practices Continued
Abstraction in Program Development:
● 3.A: Generalize data sources through variables.

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?

GUIDED PLAY AND INDEPENDENT PRACTICE


2 What activities are you still working on?

CHECK-IN & CONCEPT REVIEW


3 Are you having trouble with a concept?

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.

💎 Debug a function definition.


Songs

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!

There are two steps you


must take when using
functions in a program:
1. Define a function
○ Done once!
2. Call a function
○ Done as many
times as needed.
Python
Defining Functions Syntax
Start with this Choose a unique
keyword and valid name
1 1 Don’t forget the
1 2 colon! Don’t
forget to indent!

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!

Let’s put these


concepts into
action!
Game Recap: Village Rover

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.

● Define: The act of creating a new function.

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?

Hint: Think about how


functions allow us to
reuse blocks of code.
Wrap-Up 2
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?


Python
Wrap-Up 3
Consider the following functions:
Algorithm A: Algorithm B:

Do these functions produce the same output if called? If yes, what


is the output of both. If no, explain how they differ.
Python
Wrap-Up
Add your own
formative question.

Python

You might also like