Getting Started (5 min)
- Show first 3:30 of Function Video: http://interactivepython.org/runestone/static/thinkcspy/Functions/functions.html
- Students respond in their journals to the following prompts.
- Open Getting Started Unit 2-12.ppt in Teacher Resources when reviewing the questions below with the students (allow students to share their responses before displaying the answers on the ppt).
- What is a function?
- Why are they used?
- What is a parameter?
- What are two types of parameters?
Guided Activities (35 min)
Arguments and return values [5 min]
Point out: Data values can be stored in simple variables, lists of items, or standalone constants and can be passed as input to, or output from, procedures.
- Students read Python for Everybody Chapter 4 Functions sections 4.1 to 4.3.
- Students copy the sentences below. Replace the words argument and result with synonyms.
- “It is common to say that a function "takes" an argument and "returns" a result. The result is called the return value.”
- Visit this web page (https://docs.python.org/2/library/functions.html) that lists the built in functions in Python.
- Verify that the Python Version is 2.7.
- Ask, "How many built in functions are in Python 2.7?"
- Change the Python version to 3.4.1. How many built-in functions are in Python 3.4.1?
- Why do you think there are a different number of functions in Python 3.4.1 than in 2.7?
Compound Functions [5 min]
- Open Runestone Interactive Functions page (http://interactivepython.org/runestone/static/thinkcspy/Functions/functions.html) and read about user defined functions in Python.
- Read through lines 1 and 2 that define the header and body of compound statements. Note the format of the compound statement used to define a function.
- Ask:
- What color is used to highlight the keyword def?
- What punctuation mark ends the first( header) line?
- How far is the body indented?
- What color is the name of the function?
- What word is used for the value in parenthesis?
Function Definitions [5 min]
- Run ActiveCode 1.
- Change the number in line 15 and run the code to make a smaller square.
- Change the number in line 15 and run the code to make a larger square.
- Run ActiveCode 2.
- Ask: What are the two line numbers used to call or invoke the function drawSquare?
- Run ActiveCode 3.
- Change the definition of drawMulticolorSquare to different colors.
- Ask: What line would you change to draw smaller multi-color squares?
Return Values [10 min]
- Some functions find and return values. Complete ActiveCode 5, 6 and 7.
- Change ActiveCode 7 so it prints the minimum values.
Line Numbers [10 min]
- Run CodeLens 1.
- Ask:
- What line is executed next after line 6?
- What line is executed next after line 3?
- How do you think Python knew what line to go to after line 3?
- What keyword is used to tell Python to return a value from the function named square?
- What do you think would happen if you run the program after removing the return statement from line 3?
Exam Reference Sheet
Say: The exam reference sheet uses the name PROCEDURE instead of the Python word function. It provides:
- procName (arg1, arg2, …) as a way to call PROCEDURE procName(parameter1, parameter 2, …)
- Procedure DISPLAY(expression) to display the value of expression, followed by a space.
- Procedure INPUT(), which accepts a value from the user and returns the input value.
- result ← procName(arg1, arg2, …) to assign to result the value of the procedure “procName”.
- PROCEDURE procName(parameter1, parameter2, …) { } which is used to define a procedure that takes zero or more arguments and does not return a value.
- RETURN(expression) statement, to return control to the point where the procedure was called and return the value of expression.
- PROCEDURE procName(parameter1, parameter2, …) { RETURN(expression) } which is used to define a procedure that takes zero or more arguments to return control to the point where the procedure was called and return the value of expression.
Say: The entire exam reference sheet will be given to students when they take the end of course exam.
.
Wrap Up (10 min)
- Return to Python for Everybody Chapter 4 Functions Copy and paste the code from section 4.4 to Runestone ActiveCode 4.
- Run the code multiple times.
- Ask:
- What do you note about the results?
- What is the name of the function used?
- Is the function on the list of the built-in Python functions or is it defined in the program?
- Where do you think the function is defined?
- Add the number 1 as a parameter in the function call and run the program.
- Explain the error message.
- Write one thing to remember about functions on an exit note.
Homework: Students should read Python for Everybody Chapter 4: Sections 4.7, 4.8 and 4.9. Complete Exercises 2 and 3.
Answer the following questions:
- What error message occured in Exercise 3?
- Why did the error occur?
- What happened when you change the order of print_lyrics and repeat_lyrics function definitions in Exercise 3?
- How is a function call like a road detour?
- The parameter used to define the function print_twice is named bruce. How would the function behavior change if the name was changed to wayne all three times it appears in the function definition?
Students should be paired through this exercise since paired discussion is used for formative assessment. Question can be provided to students through a variety of formats including production of a Google form or using student response systems.
Three suggested strategies are:
- provide guide questions for reading
- provide teacher provided annotated text excerpts
- Students annotate text while reading