Skip to main content

Section 6.18 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 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.

Subsection 6.18.1 Print and Function Basics

Look at the code below and predict what it will print.

Checkpoint 6.18.1.

Q-1: What is the first thing that will be printed when the code below runs?

Checkpoint 6.18.2.

Q-2: What is the second thing that will be printed when the code below runs?

Checkpoint 6.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.

Checkpoint 6.18.4.

Run the code below to see what it prints and then use it to answer the following questions.

Note 6.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.

Checkpoint 6.18.6.

Q-5: What is the value of b when function test starts executing?

Checkpoint 6.18.7.

Q-6: What keyword in Python is used to return a value from a function?

Checkpoint 6.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.

Checkpoint 6.18.9.

Q-8: What value is returned from a function that doesn’t have a return keyword?

Checkpoint 6.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.

Subsection 6.18.2 Parts of a Function and Function Calls

Checkpoint 6.18.11.

Checkpoint 6.18.12.

Checkpoint 6.18.13.

Subsection 6.18.3 Writing Function Calls

Checkpoint 6.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.

Checkpoint 6.18.15.

Modify the code below to print “Hello Students” by adding one line and without calling the print function in that line.

Subsection 6.18.4 Function Order

Checkpoint 6.18.16.

Q-15: Look at the Python code below. What do you think will happen when you run the following code?

Checkpoint 6.18.17.

Run the code below to see what happens when you try to call a function before it is defined.

Note 6.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.

Checkpoint 6.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.

Subsection 6.18.5 Special Characters and Keywords

Checkpoint 6.18.20.

Q-18: What character indicates that what follows next is the body of the function?

Checkpoint 6.18.21.

Q-19: What character starts a comment?

Checkpoint 6.18.22.

Q-20: What Python keyword is used to start a function definition?
https://cspogil.org/Home