Close
Close full mode
logoMakeCode AP CSP

Writing Functions with Return Values

Activity

Tasks

A. Go to Activity: Return Values. Complete student tasks 1-2.

Task 1

  1. Create a function named aName.
  2. Add a return value to aName, which returns β€œBill”.
  3. After calling the function, store the returned value in the variable theName.
  4. Use game.splash to display theName.

Shared project link:

Task 1 Challenge

  1. Give aName a parameter name of type string.
  2. Modify the return value to return name instead.

Shared project link:

Task 2

  1. Create a function named addOrRemove.
  2. Add two parameters to the function: input of type number, and up of type Boolean.
  3. Add an if ... else to the function.
  4. if up is true, return input plus one, otherwise, return input minus one.

Shared project link:

Task 2 Challenge

Create four test cases for addOrRemove, with different values for input and up that verify the function is working properly.

Shared project link:

B. Based on what you have learned about return values, complete the tasks below:

Tasks

Task 3

Using the Pizza Allowance program as a baseline to create an original return value functions program.

Pizza Allowance code sample

Shared project link:

Extension

Recreate the return values tasks using JavaScript or Python.

Shared project link:

Apply (optional)

Review your past projects for opportunities to use functions with return values.

Shared project link:

Reflection

  1. What needs to be added to a function so that it will return a value?
  2. If a function returns a value in one case, does it need to return a value in all cases?
  3. What happens if you call a function with a return value without storing or using the returned value?
  4. Where might return values be used in your previous projects?

Homework

  • Complete the problem set at Problem Set: Returns.
  • You may use block or text based programming for the problem sets.
  • Comment your code as your work.
πŸ“˜ Unit 5 - AAP Part 2 β€” Previous
Return Values
Next β€” πŸ“˜ Unit 5 - AAP Part 2
Day 4