Programming fundamentalsLesson 4 of 6 in the course Functions

Python Function Parameters, Arguments, and Mutable Inputs

5 flashcards · 4 quiz questions · 5 match pairs · 3 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

Functions can receive information from the code that calls them. Understanding how that information is passed—and whether a function can change it—is essential for writing predictable Python programs.

A parameter is a variable listed in a function definition. It represents an input that the function can use. An argument is the actual value supplied when the function is called.

All flashcards

What is the difference between an argument and a parameter?
A parameter is a variable named in a function definition. An argument is the value supplied for that parameter in a function call.
How are positional arguments assigned to parameters?
They are assigned from left to right, so the first argument goes to the first parameter, the second to the second, and so on.
What does it mean for an object to be mutable?
A mutable object can be changed after it is created. A list is mutable, so in-place changes made through a function parameter can be seen by the caller.
Why doesn't assigning a new string to a function parameter change the caller's variable?
The assignment makes the local parameter refer to a different string. It does not reassign the caller's variable, and strings are immutable.
How can a function print bonus-adjusted scores without changing the input list?
Loop through the scores and calculate `score + bonus` for display, without assigning the result back into the list.

Quiz questions

  1. In `def greet(name):`, what is `name`?Answer: A parameter in the function definition.
  2. Why might a function receive a value as an argument instead of relying on a global variable?Answer: The input is explicit, and the function can use the value provided by its caller.
  3. Why does `print_div(num_1)` cause an error when `print_div` defines `op_1` and `op_2`?Answer: The call supplies only one argument, but the function requires two.
  4. A function assigns `unit = "C"` to a parameter that received the string in the caller's variable `metric`. Why does `metric` keep its original value?Answer: The assignment rebinds the local parameter; it does not change the caller's variable.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons