Writing Functions with Parameters
Activity
Go to Activity: Parameters. Apply what you have learned regarding functions with parameters to complete student tasks 1-3.
Tasks
Task 1
- Create a function named greet.
- Add a parameter firstName that is of type string.
- In the function, splash a greeting that includes firstName (if βYouβ is passed, it might splash βHey, You!β).
- Call the function with three different names.
Shared project link:
Task 2
- Start with your solution to task #1.
- Modify the function so that it accepts two strings: firstName and lastName.
- Make use of both parameters in the greeting (for example, greeting ("You", "Friend") could result in βHey You Friend!β).
Shared project link:
Task 3
- Create a function named horizontalMovement.
- Add two parameters to the function: player of type Sprite, and left of type Boolean.
- if left is true, move player 10 pixels to the left.
- if left is false, move player 10 pixels to the right.
Extension
Recreate the parameters tasks using JavaScript or Python.
Shared project link:
Apply
Review your past projects for opportunities to use functions with parameters.
Shared project link:
Reflection
- How do parameters allow for more flexible code?
- Review the functions you have learned, like splash and say, and identify which ones have parameters.
- Where might parameters be used in your previous projects?
Homework
- Complete the problem set at Problem Set: Parameters.
- You may use block or text-based programming for the problem sets.
- Comment your code as your work.