04 Functions Flashcards
What are the advantages of using functions over repeating/copying code?
- less prone to errors
- less lines of code to read
- easier to maintain, changes only have to be done at one place instead of multiple places
True or False: Functions in Python always have a return value.
False, the output of a function is optional.
True or False: Not every function has to have input parameters.
True
What’s the difference between formal and actual parameters?
Formal: in the function definition
Actual: when calling the function (also called arguments)
True or False: If a list is passed to a function and then changed in the function body, these changes aren’t reflection outside the function.
False. If a mutable object (ie a list) is changed inside a function, the change is also reflected outside of the function.
What’s the difference between positional and keyword arguments?
- Positional: They don’t need to appear with a keyword. Instead the position of the argument determents to which parameter it belongs.
- Keyword: They need a keyword to specify to which parameter it belongs. They can be at any position and in any order, as long as they’re not placed before a positional argument.
What are variable arguments?
They are placeholders for an arbitrary amount of arguments.
For positional arguments: *args
For keyword arguments: **kwargs
In what order can variable arguments, mixed with normal parameters appear?
- zero or more normal parameters can appear before *args
- zero or more normal parameters can appear after *args but only if accessed via keyword
- no parameter can appear after **kwargs
- normal parameters can never be part of *args or **kwargs
For the defined function “func”, what arguments belong to what parameters in the following examples?
def func(x, *args, y, **kwargs):
- func(1)
- func(1, 2)
- func(1, y=2)
- func(1, 3, y=2)
- func(1, z=4, y=2)
- Error: value for y is missing.
- Error: value for y is missing, because 2 is assigned to *args
- x=1, args=(), y=2, kwargs={}
- x=1, args=(3, ), y=2, kwargs={}
- x=1, args=(), y=2, kwargs={“z”: 4}
For the defined function “func”, what arguments belong to what parameters in the following example?
def func(x, *args, y, **kwargs):
my_list = [1, 2, 3]
my_dict = {“y”: 4, “z”: 5}
func(*my_list, **my_dict)
x=1, args=(2, 3), y=4, kwargs={“z”: 5}
What are default argument values?
A default value can be assigned to a parameter when defining a function. If the function is called without an argument, the default argument is used instead.
What are the parameter values for the function “func” for the following examples?
def func(amount=0, price=3):
- func()
- func(2)
- func(2, 1)
- func(amount=2)
- func(price=2)
- func(price=2, amount=1)
- func(2, price=2)
- amount=0, price=3
- amount=2, price=3
- amount=2, price=1
- amount=2, price=3
- amount=0, price=2
- amount=1, price=2
- amount=2, price=2
What is type hinting?
Parameters and return values can have type hints that show what your function expects as input and returns as output.
They’re only hints though, you can still pass and return any type.
True or False: Once the return keyword is encountered in a function, the program is terminated.
False. Only the execution of the function is terminated and the output is returned.
What namespaces are there and in what order are they searched when looking for a name of an object?
- local namespace
- enclosing/nested namespaces
- global namespace
- built-in namespace