← Back to course
2/6
Week 06 · Procedures and Abstraction

Tuesday

Parameters, arguments and RETURN
// Splitting a big problem into pieces you can reuse
⏱ about 20 min

Tuesday: Parameters, Arguments and RETURN

Comet writes a new procedure called checkPlant. It decides whether a plant needs water.

She calls it three times, once for the tomatoes, once for the basil and once for the beans.

"Same procedure," she says, "three different answers."

Wren points at the word level inside the parentheses. "Then where does level get its value?"

Comet points to the calls. "From the number I send each time."

Nova projects two glowing labels, one over the definition and one over a call.

"Interesting," she says. "Those two things have two different names. Can you find out what they are?"

Parameters and arguments

Parameters are the input variables of a procedure. They are named when the procedure is written.

Arguments are the values given to the parameters when the procedure is called.

Using parameters lets one procedure be reused with many different input values.

PROCEDURE checkPlant(level)
{
  IF (level < 30)
  {
    status ← "needs water"
  }
  ELSE
  {
    status ← "all good"
  }
  RETURN(status)
}

tomatoStatus ← checkPlant(25)
basilStatus ← checkPlant(64)
beanStatus ← checkPlant(30)
In checkPlant, the parameter is ____.
In the call for the tomatoes, the argument is ____.
A value given to a parameter in a call is an ____.
TRACE THE CALLS
  • Read the question.
  • Tap your answer.
What does tomatoStatus get?
What does basilStatus get?
What does beanStatus get?
How many times is the procedure called in this code?

RETURN sends a value back

A procedure that returns a value works like an expression. The call is replaced by the value it returns.

In the line below where lightAverage gets average(lightReadings), lightAverage stores whatever average sends back.

lightReadings ← [80, 90, 70]
lightAverage ← average(lightReadings)
tempAverage ← average([20, 22, 24, 22])
WHAT COMES BACK?
  • Read the question.
  • Tap your answer.
What is lightAverage?
What is tempAverage?
Inside average, what is count after it handles lightReadings?
StatementTrue or false?
A parameter is named when the procedure is written.?
An argument is the value passed in when the procedure is called.?
A procedure with a parameter can work with only one input value.?
A call to average can be stored in a variable.?
WHY THIS EXERCISEParameters and return values are how procedures take data in and send results out.
Try it
Write a procedure on paper called double that has one parameter, number, and returns number times 2.
Trace three calls: double(4), double(10) and double(0).
WHAT HAPPENS WHEN TOMATOSTATUS GETS CHECKPLANT(25) RUNS
  • ?The body checks whether level is less than 30.
  • ?status gets "needs water" and RETURN sends it back.
  • ?Beacon reaches the line with the call checkPlant(25).
  • ?The parameter level gets the argument 25.
  • ?tomatoStatus gets the returned value.
WHY THIS EXERCISEFollowing a call step by step shows how data flows into a procedure and back out.
On paper, draw arrows from each checkPlant argument to the parameter level, and from RETURN back to each variable.

Nice work. Tomorrow in Beacon Lab you will split all of Beacon into modules.

← Monday