u04-slides-functions-flashcards
Why should we avoid code duplication?
It’s prone to errors, makes programs longer to read, and is more difficult to maintain
What is the naming convention for functions in Python?
Lowercase letters with underscores if needed
What are formal parameters in a function?
The parameters specified in the function definition
What are actual parameters (arguments) in a function?
The parameters specified when calling the function
How are immutable arguments passed to functions?
By call by value
How are mutable arguments passed to functions?
By call by reference (with value being the object reference)
What are positional arguments?
Arguments passed to a function based on their position, without explicitly naming parameters
What are keyword arguments?
Arguments passed to a function by explicitly naming parameters (e.g., param=value)
Can positional arguments appear after keyword arguments?
No
What does *args do in a function definition?
Collects arbitrary positional arguments into a tuple
What does **kwargs do in a function definition?
Collects arbitrary keyword arguments into a dictionary
What is parameter unpacking?
Using * to unpack iterables into positional arguments, or ** to unpack dictionaries into keyword arguments
How do you specify default parameter values?
In the function definition with an equals sign (e.g., def func(param=default_value))
What’s important to know about mutable default parameters?
They are evaluated once at function definition and can be changed across calls
What is type hinting in Python?
Optional hints about parameter and return value types (e.g., def func(x: int) -> int)