5.17. Group Work: Functions¶
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.
Students will know and be able to do the following.
Learn how to display a value in Python using print.
Recognize a function definition, function header, function body, function name, default value for a function parameter, and function call in a program.
Predict output from functions.
Recognize common symbols (:, #), operators (+, -, /), and keywords (def, return) in Python.
Recognize that indentation is important in Python
Modify code that includes function definitions and function calls
Learn how to properly indent the body of a function
5.17.1. Print and Function Basics¶
Look at the Python code below and then answer the following questions.
Run the code below to see what it prints.
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.
Delete the last line of the test function above and run the code again.
5.17.2. Parts of a Function and Function Calls¶
Modify the code below to print the last two lines twice by only adding one line. That line can’t call the print function.
Modify the code below to print “Hello students” by adding one line and without calling the print function in that line.
5.17.3. Function Order¶
p9-14: Look at the Python code below. What do you think will happen when you run the following code?
Run the code below to see what happens when you try to call a function before it is defined.
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.
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.