Parameters and Arguments

Parameters name inputs in a definition. Arguments supply the values that reach those names at a particular call.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Bind inputs at the call
  2. Make noncommutative inputs clear
  3. Bind each supplied expression before executing the body
  4. Treat the value and its object separately

Bind inputs at the call

If area declares width and height, calling it with 3 and 5 binds width to 3 and height to 5 under ordinary positional rules. This is part of name binding. The function body can then compute fifteen.

Make noncommutative inputs clear

The order of width and height is invisible in an area calculation. A subtraction function produces opposite signs when its inputs are swapped. Keyword arguments or a call in a test with distinct values can expose the intended roles.[1][2]

Bind each supplied expression before executing the body

In paint(width, height, coats=1) there are three parameters. The call paint(4, coats=2, height=3) first evaluates the supplied values and binds width to four, coats to two and height to three. Inside the body their order in the call has no effect on the calculation \(4\cdot3\cdot2=24\). A caller using three positional arguments must follow declaration order. Test a mixed call and one that gives height by both position and name, which Python rejects during binding. See using keyword arguments.

Treat the value and its object separately

Passing a list supplies a reference to that object in Python. Reassigning the parameter to an empty list changes where the local name points. Calling clear() on it modifies the caller-visible object if both names still refer to that list. Trace both examples using the same initial list [3, 1], then inspect the caller's variable after each call. This distinction is part of the public interface even when both implementations return the same result. See input ownership.

References

  1. Python Software Foundation, Python Tutorial: Control Flow ↩
  2. Python Software Foundation, Python Language Reference: Execution Model ↩