Vous êtes sur la page 1sur 12

ECT Lesson Plan: Functions and Algorithms

Lesson plan at a glance...

In this lesson plan

Core subject(s)

Computer Science

Subject area(s)

Software Development Fundamentals

Suggested age

13 to 18 years old

Prerequisites

None

Lesson Overview
Materials and Equipment
Preparation Tasks
The Lesson
Learning Objectives and Standards
Additional Information and Resources

Time

Preparation: 5 to 15 minutes
Instruction: 80 to 115 minutes

Standards

Core Subject: CCSS Math


CS: CSTA

Lesson Overview
In computational thinking, the patterns we recognize in the world can be abstracted into functions. If we have a function
we can give it the ability to loop quickly, use logic to make decisions, and more. By designing algorithms into functions
we are able to better understand and use the laws of our world. Students will be able to identify, evaluate, follow and
create functions including functions that loop, functions that include decisions and functions that include both. The
activities increase in difficulty and students should continue as far as they are able to.

Materials and Equipment

For the student:


Required: Internet-connected computers (one (1) computer per student recommended)
Required: Software Development Environment
Python 2.x (https://www.python.org/) OR a web-based Integrated Development Environment (IDE) such as Trinket
(https://trinket.io/)

Preparation Tasks
Confirm that all students computers are turned on, logged-in, and connected to
the Internet

1 to 10 minutes

Confirm that Python is installed (https://www.python.org/), or navigate to Trinket


(https://trinket.io/)

3 to 10 minutes

The Lesson
Warm-up Activity: What are functions and algorithms?

15 to 20 minutes

Activity 1: Creating functions

15 to 20 minutes

Activity 2: Functions that repeat or loop

20 to 30 minutes

Activity 3: Functions that use logic to make decisions

20 to 30 minutes

Wrap-up Activity: Writing your own function

10 to 15 minutes

ECT: Functions and Algorithms

1 of 12

ECT: Functions and Algorithms

2 of 12

Warm-up Activity: What are functions and algorithms? (15 to 20 minutes)


Activity Overview: In this activity, students will be introduced to the fundamental concepts of functions and algorithms
using meaningful and relevant content.
Notes to the Teacher:
Even if students are not aware of it, functions are doing some kind of calculation with the data.
If your students are stuck, you may share one or more examples of functions, such as:
Its cold today so I will put on a jacket.
The drink costs 2 dollars and I have 8 quarters.
I have a project due in a week so I need to spend 30 minutes on it tonight.
A picture from my phone is too dark so Ill use a computer program to brighten it.

Activity:
Write the following definitions of function and algorithm in a place where your students may refer back to it:
Functions take input data like numbers, words, pictures, etc. and return an output.
Algorithms are the instructions, steps, or recipe to get from the inputs to the output.
4-S Brainstorming: All students respond to the following prompt provided by the teacher.
Write an algorithm that provides an example of a function from your everyday life.
Four students will be selected by the teacher to play a specific role in helping the rest of the class generate the
maximum number of responses.
Accelerator - Student encourages classmates to generate more ideas (Lets get more ideas, only two minutes left)
Acceptor - Student helps classmates commit to an idea (All ideas are OK, write that one down)
Exaggerator - Student encourages classmates to generate different kinds of ideas (We need some silly ideas)
Connector/recorder - Student makes the connection between different ideas and writes them down (Which ideas are
connected to that?)
The teacher will facilitate the activity
After 10 minutes the teacher will collect the responses. Key themes related to the activity will be written on the board.

Activity 1: Creating functions (15 to 20 minutes)


Activity Overview: In this activity, students generalize addition beyond mathematics and design algorithms for
repeatable conversion formulas.
Notes to the Teacher:
As in math and science, there are functions which are used over and over again. By learning a few basic types of
functions, its possible to perform a wide variety of algorithms and simulations. The simplest functions are those that
take an input, perform a calculation and return an output. These functions only run one time.

Activity:
Have students do the following and and answer questions:
Run the examples below in your Python Interpreter. >>> is the interpreter prompt, so you type what comes
after >>>. If your interpreter has color, the program prints the blue text. If the program types a question, you
type an answer and press Enter.

ECT: Functions and Algorithms

3 of 12

# Lines that start with a # are comments and are ignored by Python.
>>> 2 + 5
7
>>> r = input("What is the radius of your circle? ")
What is the radius of your circle? 10
>>> a = 3.14*(r**2) #pi times r squared
>>> print "Area = " + str(a)
314.0
>>> name = input("What is your name? ")
What is your name? "Arthur"
>>> print "Hello " + name
Hello Arthur
There are 5 functions in the examples above can you find them all?
Why did we need to convert a into a string of characters in print "Area = " + str(a)?
Rewrite the third example to calculate circumference instead.
Create a function that converts units (e.g. meters feet, Celsius Fahrenheit).
Create a function that asks for your age and prints out how many seconds you have been alive.
Q1: There are 5 functions in the above examples, can you find them all?
Q2: Why did we need to convert a into a string of characters in print "Area = " + str(a)?
Q3: Rewrite the third example to calculate circumference instead.

Student Assessment:
A1: Addition "+" (of numbers and strings), input(), multiplication "*", print, and str().
A2: The number a and the words Area are not the same type and needed to be converted to show on the same line.
A3: c = 3.14*2*r
print "Area = " + str(c).

Activity 2: Functions that repeat or loop (20 to 30 minutes)


Activity Overview: In this activity, students explore the idea of repeating an activity multiple times, both as a thought
exercise and in code.
Activity:
Functions are helpful for making decisions or calculations.
Answer the following questions:
Q1: How do you deal with activities or chores in your life that repeat?
Q2: What are examples of functions that repeat?

Assessment:
A1: Possible answers:

ECT: Functions and Algorithms

4 of 12

Alarm Clocks or Calendar Reminders


Music (singing the chorus over and over)
A cars combustion cycle (intake, compression, combustion, exhaust)
A2: Answers will vary

Activity:
Take students through the following with student-teacher interaction:
The sample assessment answers illustrate three types of repeating function:
Loops forever (every day or every year)
Loops a certain number of times (sing the chorus 3 times)
Loops while something is true (while there is gasoline and the car is on)
When writing a loop, the initial line ends with a colon ":". The next lines requires four spaces of indentation to
show which code is inside the loop and which code is not. Other languages use brackets { } to do this. In IDLE,
Python will automatically index the code as long as you include the colon.
Example of a loop that continues forever (infinite loop):
>>> count = 0
>>> while 1 == 1: #The loop will repeat forever until you stop Python.
...
count += 1 #Adds 1 to count each time it loops
...
print count
1
2
3
... #and so on forever - to stop hold control key and touch the c key
Q1: Can you imagine a few other ways to create an infinite loop with the while loop?
Q2: Why does count = 0 use only one equals sign but while 1 == 1: uses two?
Q3: What is another way count += 1 could be written?

Assessment:
A1: while 3 == 3, while 1, while True, etc. As long as the test is always true.
A2: The first is storing zero in the variable count; the second is a test to see if one is equal to one.
A3: count = count + 1

Activity:
Share the following with your students:
Example of a loop that only repeats a few times:
>>> count = 3
>>> while count > 0: #Checks each time to see if count is greater than
zero.
...
print count
...
count -= 1 #Subtracts 1 from count each time it loops
3

ECT: Functions and Algorithms

5 of 12

2
1
>>> for count in range(3, 0, -1): #Another way to say the same as above
in Python
print count
Q4: In the second version of the loop above, why is the range from 3 to 0 when the loop prints out the numbers
3, 2, 1?
Q5: For the second loop, change it to for count in range(1,6,2): what does the third number do? What
would it do if it was changed to 3?

Assessment:
A4: The function is range(start,end) including start but excluding end.
A5: The third number is the step or how many numbers to skip each time. If it was changed to 3 then the output would
be 1 and 4.

Activity 3: Functions that use logic to make decisions (20 to 30 minutes)


Activity Overview: In this activity, students look at interdisciplinary connections to CS by thinking about functions that
are not completely about math and understanding that computational thinking extends beyond math and computer
science.
Notes to the Teacher:
Students will use Boolean logic for this activity.

Activity:
Share the following with your students:
The functions so far use calculations. Functions can also use logic to make decisions and take different
actions based on these decisions. When you combine repeating algorithms and logic, there is enough
intelligence to make decisions. That is the power of computational thinking.
Humans use their own logic in the functions they use everyday:
Which shirt matches my socks?
How much sugar and cream should I add to my coffee to make it perfect?
What is the fastest way to school while staying dry in the rain?
Example of logic that determines which name is first in alphabetical order:
>>> name1 = input("What is the first name? ")
What is the first name? "Alice"
>>> name2 = input("What is the second name? ")
What is the second name? "Bob"
>>> if name1 < name2:
...
print name1
else:
...
print name2
Alice

ECT: Functions and Algorithms

6 of 12

Example of a Guess The Number Game (you may wish to save the code so you can play it again):
>>> from random import *
>>> correct_number = randint(1,10) #Computer picks a number from 1 to
10
>>> guess = 0 #This is set to zero so the loop begins.
>>> while guess != correct_number:
...
guess = input("What number am I thinking of (between 1-10)? ")
...
if guess > correct_number:
...
print "Too high"
...
if guess < correct_number:
...
print "Too low"
...
if guess == correct_number:
...
print "You got it!"
Example output:
What number am I thinking of (between 1-10)? 5
Too High
What number am I thinking of (between 1-10)? 3
Too Low
What number am I thinking of (between 1-10)? 4
You Got it!
Q1: Why does the first loop put the inputs in alphabetical order with the < sign? Isnt < only for numbers?
Q2: What would happen if you ran the Guess The Number game again but skipped the second and third lines?

Assessment:
A1: Operator overloading is when the < sign can compare 1 < 2 and 'a' < 'b'. Another example of operator
overloading is 2 + 3 and 'Py' + 'thon'.
A2: The game would not pick a new random number, guess would not be reset to 0, therefore the condition to run the
loop would not be met. Python would also complain that 'guess' is not defined.

Activity:
Share the following with your students:
Here is an example of a loop that acts like a Thermostat in your home:
>>> set_temp = 70
>>> current_temp = 70
>>> while 1 == 1:
...
set_temp = input("What do you want the temperature to be? ")
...
if current_temp > set_temp:
...
print "Turning on the air conditioning!"
...
while current_temp > set_temp:
...
current_temp -= 1
...
print "Current Temp: " + str(current_temp)
...
if current_temp < set_temp:
...
print "Turning on the heater!"
...
while current_temp < set_temp:
...
current_temp += 1
...
print "Current Temp: " + str(current_temp)
Q3: Instead of having the infinite loop in the beginning of the code, how could you modify the code to only run

ECT: Functions and Algorithms

7 of 12

once?
Q4: Imagine you had a more powerful air conditioning or heater that added two degrees each time through the
loop instead one. What might be a possible issue?

Assessment:
A3: There are many possible options but the easiest one would be to modify the first loop to say
while current_temp != set_temp.
A4: If nothing else was changed in the code, you might overshoot the set_temp. This already happens in your homes
but negative feedback loops and sensors work to prevent this.

Wrap-up Activity: Writing your own function (10 to 15 minutes)


Notes to the Teacher:
You can share one or more of these function ideas with your students to get them started:
Physics: Projectile motion, decoding resistor values, friction/forces
Biology: Population growth and decay, converting DNA into amino acids, taxonomy
Chemistry: pH balance, titration, radioactive decay
Unit conversion, graphing
Activity:
Have students do the following:
Create a function for a content area related topic, using either code, Pseudocode, or a descriptive paragraph.
(Pseudocode is helpful for reviewing concepts.)

Learning Objectives and Standards


Learning Objectives

Standards

Location

LO1: The student will be able to describe


both a mathematical and a nonmathematical example of a function

Core Standards
CCSS MATH.PRACTICE.MP4: Model with mathematics.

Warm-up
Activity

CCSS MATH.CONTENT.8.F.A.1: Understand that a


function is a rule that assigns to each input exactly one
output. The graph of a function is the set of ordered pairs
consisting of an input and the corresponding output.
CCSS MATH.CONTENT.8.F.A.2: Compare properties of
two functions each represented in a different way
(algebraically, graphically, numerically in tables, or by
verbal descriptions). For example, given a linear function
represented by a table of values and a linear function
represented by an algebraic expression, determine which
function has the greater rate of change.
CCSS MATH.CONTENT.8.F.A.3: Interpret the equation y

ECT: Functions and Algorithms

8 of 12

= mx + b as defining a linear function, whose graph is a


straight line; give examples of functions that are not
linear. For example, the function A = s2 giving the area of
a square as a function of its side length is not linear
because its graph contains the points (1,1), (2,4) and
(3,9), which are not on a straight line.
Computer Science
CSTA L2.CT.14: Examine connections between elements
of mathematics and computer science including binary
numbers, logic, sets and functions.
LO2: The student will be able to identify
functions used within a Python program

Core Standards
CCSS MATH.CONTENT.8.F.A.1: Understand that a
function is a rule that assigns to each input exactly one
output. The graph of a function is the set of ordered pairs
consisting of an input and the corresponding output.

Activity 1

CCSS MATH.CONTENT.8.F.A.2: Compare properties of


two functions each represented in a different way
(algebraically, graphically, numerically in tables, or by
verbal descriptions). For example, given a linear function
represented by a table of values and a linear function
represented by an algebraic expression, determine which
function has the greater rate of change.
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.
CCSS MATH.PRACTICE.MP7: Look for and make use of
structure.
Computer Science
CSTA L2.CT.14
LO3: The student will be able to modify a
Python program to calculate a different
formula given the same input(s)

Core Standards
CCSS MATH.CONTENT.8.F.B.4: Construct a function to
model a linear relationship between two quantities.
Determine the rate of change and initial value of the
function from a description of a relationship or from two
(x, y) values, including reading these from a table or from
a graph. Interpret the rate of change and initial value of a
linear function in terms of the situation it models, and in
terms of its graph or a table of values.

Activity 1

CCSS MATH.PRACTICE.MP2: Reason abstractly and


quantitatively.
CCSS MATH.PRACTICE.MP7: Look for and make use of
structure.
Computer Science
CSTA L2.CT.14
LO4: The student will be able to trace
through a while loop to determine the
output.

ECT: Functions and Algorithms

Core Standards
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.

Activity 2

9 of 12

CCSS MATH.PRACTICE.MP7: Look for and make use of


structure.
Computer Science
CSTA L2.CT.14
LO5: The student will be able to identify the
values in a Python for loop range

Core Standards
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.

Activity 2

CCSS MATH.PRACTICE.MP7: Look for and make use of


structure.
Computer Science
CSTA L2.CT.14
LO6: The student will be able to identify
which of two strings precedes (is less
than) the other.

Core Standards
CCSS MATH.PRACTICE.MP2: Reason abstractly and
quantitatively.

Activity 3

CCSS MATH.PRACTICE.MP7: Look for and make use of


structure.
Computer Science
CSTA L2.CT.14
LO7: The student will be able to create a
function to solve a conversion problem.

Core Standards
CCSS MATH.CONTENT.8.F.B.4: Construct a function to
model a linear relationship between two quantities.
Determine the rate of change and initial value of the
function from a description of a relationship or from two
(x, y) values, including reading these from a table or from
a graph. Interpret the rate of change and initial value of a
linear function in terms of the situation it models, and in
terms of its graph or a table of values.

Wrap-up

CCSS MATH.PRACTICE.MP2: Reason abstractly and


quantitatively.
CCSS MATH.PRACTICE.MP1: Make sense of problems
and persevere in solving them.
Computer Science
CSTA L2.CT.14

Additional Information and Resources


Lesson Vocabulary
Term

Definition

For Additional Information

Location

Function

Takes inputs, makes a calculation or


decision, and produces an output.

http://en.wikipedia.org/wiki/Function

Warm-up
Activity

ECT: Functions and Algorithms

10 of 12

Algorithm

The instructions or logic that convert a


functions inputs into outputs.

http://en.wikipedia.org/wiki/Algorithm

Warm-up
Activity

Boolean logic

Compares two objects and determines


whether the statement is true or false.
Some examples: <,>, ==, !=.

http://en.wikipedia.org/wiki/Boolean_alg
ebra

Activity 3

Python
interpreter/shell

In this mode, Python handles one line of


instruction at a time as opposed to
creating a multiline program and running
it all as one. You will know you are in
Pythons interpreter mode if you see
>>>.

https://docs.python.org/2.7/tutorial/inter
preter.html

Activity 1

Negative
feedback

Negative feedback occurs when some


function of the output of a system,
process, or mechanism is fed back in a
manner that tends to reduce the
fluctuations in the output, whether
caused by changes in the input or by
other disturbances.

https://en.wikipedia.org/wiki/Negative_f
eedback

Activity 3

Computational Thinking Concepts


Concept

Definition

Location

Algorithm
Design

Identifying and extracting relevant information to define main idea(s)

Warm-up
Activity

Student Engagement Strategies


Strategy

Description

Location

Meaningful and
Relevant
Content

Content that is meaningful and/or relevant to the individual student or the


discipline

Warm-up
Activity

Student-Teacher
interaction

Interactions between students and teachers that encourages students to feel less
intimidated and more included during novel tasks

Activity 2

Interdisciplinary
Connections to
CS

The integration of knowledge and skills from other disciplines (e.g. Biology,
Finance) with computer science

Activity 3

Administrative Details
Contact info

For more info about Exploring Computational Thinking (ECT), visit the ECT website
(g.co/exploringCT)

Credits

Developed by the Exploring Computational Thinking team at Google and reviewed by K-12
educators from around the world.

Last updated on

07/02/2015

ECT: Functions and Algorithms

11 of 12

Copyright info

Except as otherwise noted, the content of this document is licensed under the Creative Commons
Attribution 4.0 International License, and code samples are licensed under the Apache 2.0 License.

ECT: Functions and Algorithms

12 of 12

Vous aimerez peut-être aussi