04 Functions Flashcards

1
Q

What are the advantages of using functions over repeating/copying code?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Functions in Python always have a return value.

A

False, the output of a function is optional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or False: Not every function has to have input parameters.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the difference between formal and actual parameters?

A

Formal: in the function definition
Actual: when calling the function (also called arguments)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

False. If a mutable object (ie a list) is changed inside a function, the change is also reflected outside of the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between positional and keyword arguments?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are variable arguments?

A

They are placeholders for an arbitrary amount of arguments.
For positional arguments: *args
For keyword arguments: **kwargs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In what order can variable arguments, mixed with normal parameters appear?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

For the defined function “func”, what arguments belong to what parameters in the following examples?

def func(x, *args, y, **kwargs):

  1. func(1)
  2. func(1, 2)
  3. func(1, y=2)
  4. func(1, 3, y=2)
  5. func(1, z=4, y=2)
A
  1. Error: value for y is missing.
  2. Error: value for y is missing, because 2 is assigned to *args
  3. x=1, args=(), y=2, kwargs={}
  4. x=1, args=(3, ), y=2, kwargs={}
  5. x=1, args=(), y=2, kwargs={“z”: 4}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)

A

x=1, args=(2, 3), y=4, kwargs={“z”: 5}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are default argument values?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the parameter values for the function “func” for the following examples?

def func(amount=0, price=3):

  1. func()
  2. func(2)
  3. func(2, 1)
  4. func(amount=2)
  5. func(price=2)
  6. func(price=2, amount=1)
  7. func(2, price=2)
A
  1. amount=0, price=3
  2. amount=2, price=3
  3. amount=2, price=1
  4. amount=2, price=3
  5. amount=0, price=2
  6. amount=1, price=2
  7. amount=2, price=2
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is type hinting?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False: Once the return keyword is encountered in a function, the program is terminated.

A

False. Only the execution of the function is terminated and the output is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What namespaces are there and in what order are they searched when looking for a name of an object?

A
  1. local namespace
  2. enclosing/nested namespaces
  3. global namespace
  4. built-in namespace
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In the following example, define the namespaces of the given object names:

x = str(12)
def func(a):
c = 10
return a + c

A

x: global namespace
str: built-in namespace
func: global namespace
a: local namespace
c: local namespace