Quarry School

Pass an argument — quick review

In plain words

A printed invitation leaves a blank after the word Dear.

Rule

A parameter is a name in the parentheses of a def line, and an argument is a value in the parentheses of a call. For the calls in this unit, Python works out the arguments, ties them to the parameters in order and then runs the body. For the functions defined in this unit, a call with fewer arguments than parameters stops with a TypeError.

Step

  1. Find the value in the body that should change from call to call.

  2. Give that value a name and write the name inside the parentheses of the def line. For two values, write two names with a comma between them.

  3. Use the names in the body where the values were.

  4. In each call, write the values inside the parentheses in the same order as the names.

  5. Trace a call by writing each parameter with the argument it received, then running the body with those values.

Trap

Arguments in the wrong order are the quiet mistake. label(3, "pens") is a legal call, and the program displays pens x 3. Check each call against the order of the names in the def line.

A second slip is to put quotation marks around a parameter's name in the body. Quotation marks make text, so print("name") displays the word name for every guest. Write the name without quotation marks, or inside the braces of an f-string, to use the value it holds.