Writing Functions with Return Values
Activity
Tasks
A. Go to Activity: Return Values. Complete student tasks 1-2.
Task 1
- Create a function named aName.
- Add a return value to aName, which returns βBillβ.
- After calling the function, store the returned value in the variable theName.
- Use game.splash to display theName.
Shared project link:
Task 1 Challenge
- Give aName a parameter name of type string.
- Modify the return value to return name instead.
Shared project link:
Task 2
- Create a function named addOrRemove.
- Add two parameters to the function: input of type number, and up of type Boolean.
- Add an if ... else to the function.
- 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.
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
- What needs to be added to a function so that it will return a value?
- If a function returns a value in one case, does it need to return a value in all cases?
- What happens if you call a function with a return value without storing or using the returned value?
- 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.