Close
Close full mode
logoMakeCode AP CSP

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

  1. Create a function named greet.
  2. Add a parameter firstName that is of type string.
  3. In the function, splash a greeting that includes firstName (if β€œYou” is passed, it might splash β€œHey, You!”).
  4. Call the function with three different names.

Shared project link:

Task 2

  1. Start with your solution to task #1.
  2. Modify the function so that it accepts two strings: firstName and lastName.
  3. Make use of both parameters in the greeting (for example, greeting ("You", "Friend") could result in β€œHey You Friend!”).

Shared project link:

Task 3

  1. Create a function named horizontalMovement.
  2. Add two parameters to the function: player of type Sprite, and left of type Boolean.
  3. if left is true, move player 10 pixels to the left.
  4. 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

  1. How do parameters allow for more flexible code?
  2. Review the functions you have learned, like splash and say, and identify which ones have parameters.
  3. 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.
πŸ“˜ Unit 5 - AAP Part 2 β€” Previous
Parameters
Next β€” πŸ“˜ Unit 5 - AAP Part 2
Day 3