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)
What happens if a function doesn’t explicitly return anything?
It returns None by default
What is a namespace in Python?
A dictionary that maps names (variables functions etc.) to their objects
What are the different types of namespaces in Python?
Built-in namespace, global/module namespace, enclosing/nested namespaces, and local namespace
What is the scope lookup order in Python?
Local namespace, then enclosing namespaces, then global namespace, then built-in namespace
What’s in the built-in namespace?
Python built-in functions and objects (like len dict print etc.)
What’s in the global namespace?
Names defined in the main script/module (like global variables)
What’s in the local namespace?
Names defined at the innermost level (like local variables within a function)
What happens when a return statement is encountered?
The function immediately terminates and returns the specified value
Can you mix normal parameters with *args and **kwargs?
Yes, with specific rules about order and keyword-only arguments