It is best to use a POGIL approach with the following. In POGIL students work in groups on activities and each member has an assigned role. For more information see https://cspogil.org/Home 1 .
Learning Objectives
Students will know and be able to do the following.
Content Objectives:
Learn how to display a value in Python using print.
Math terms for functions to their definitiions.
Recognize common symbols (:, #), operators (+, -, /), and keywords (def, return) in Python.
Process Objectives:
Modify code that includes function definitions and function calls
Learn how to properly indent the body of a function
Predict output from functions.
Subsection6.18.1Print and Function Basics
Look at the code below and predict what it will print.
Checkpoint6.18.1.
Q-1: What is the first thing that will be printed when the code below runs?
Checkpoint6.18.2.
Q-2: What is the second thing that will be printed when the code below runs?
Checkpoint6.18.3.
Q-3: What is the last thing that will be printed when the code below runs?
Run the Python code below and then answer the following questions.
Checkpoint6.18.4.
Run the code below to see what it prints and then use it to answer the following questions.
Note6.18.5.
You do not declare the type of a variable in Python. Python requires that all statements that are part of the body of a function must be indented. Use four spaces to indent.
Checkpoint6.18.6.
Q-5: What is the value of b when function test starts executing?
Checkpoint6.18.7.
Q-6: What keyword in Python is used to return a value from a function?
Checkpoint6.18.8.
Q-7: What built-in function in Python is used to display a value?
Delete the last line of the test function above and run the code again.
Checkpoint6.18.9.
Q-8: What value is returned from a function that doesn’t have a return keyword?
Checkpoint6.18.10.
Drag the blocks from the left and put them in the correct order on the right to describe the order the function calls and functions execute.
Call to main function on line 21
---
main function defined on line 14
---
call to test function on line 18
---
test function defined on line 2
Subsection6.18.2Parts of a Function and Function Calls
Checkpoint6.18.11.
Click on all of the function headers in the code below.
The first line in the function definition is the header.
# function definition
def print_message():print("Welcome to Python.")print("Learn the power of functions!")
# function definition
def main():print("Hello Programmer!")
# function calls
print_message()main()
Checkpoint6.18.12.
Click on all of the function names in the code below.
The name of the function is after the def keyword and before the ().
# function definition
defprint_message():print("Welcome to Python.")print("Learn the power of functions!")
# function definition
defmain():print("Hello Programmer!")
# function calls
print_message():
main()
Checkpoint6.18.13.
Drag each term to its definition
Read the chapter on functions and try again.
function definition
All of the code that tells the program what to do when the function is executed. It includes the header and body.
function header
The first line of a function definition
function body
All of the lines in the function after the function header
function call
The name of the function followed by an argument list in ().
function name
Follows the def keyword and is before the list of parameters in ().
Subsection6.18.3Writing Function Calls
Checkpoint6.18.14.
Modify the code below to print the last two lines twice by only adding one line. That line can’t call the print function.
Checkpoint6.18.15.
Modify the code below to print “Hello Students” by adding one line and without calling the print function in that line.
Subsection6.18.4Function Order
Checkpoint6.18.16.
Q-15: Look at the Python code below. What do you think will happen when you run the following code?
Checkpoint6.18.17.
Run the code below to see what happens when you try to call a function before it is defined.
Note6.18.18.
Python processes the code in a file from left to right and from the first line to the last line. All functions must be defined before they are called.
Checkpoint6.18.19.
Drag the blocks from the left and put them in the correct order on the right to define a function print_greeting that asks for your name and prints “Hello Name”. Then define a main function that calls print_greeting. Be sure to also call the main function. Note that you will have to indent the lines that are in the body of each function. Click the Check button to check your solution.