Defining a Function with Parameters
Giving a named block of code a job of its own and passing values into it, so the same steps run on different data.
What a learner can do afterwards
- Define a function and call it more than once with different arguments
- Decide which values the function needs as parameters
- Replace a repeated block with a single call
1 · Read
A procedure packs a few steps under one name so you can reuse them. Define singBingo once, then call it three times, and the computer still runs every line each time. You type less, yet the machine does the full job.
Parameters let one recipe serve many callers. sayHello(person) greets Jackson, Mr H or Stranger depending on the value handed in. One greet replaces three copied print lines.
Design the inputs from what callers need, with few, clear names. reportGrade takes a student name and a count correct, like Sally with 27, where the score is (correct out of 30) times 100, turning 27 into 90. The count and order of values at the call must match the names in the definition.
Name each procedure for what it does, not how it works. When a fix lands in one place, every call benefits.
Pack repeated steps into a named procedure, feed each call through parameters, and call it wherever you need it.
2 · Watch
Take it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.