u04-slides-functions-flashcards

1
Q

Why should we avoid code duplication?

A

It’s prone to errors, makes programs longer to read, and is more difficult to maintain

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

What is the naming convention for functions in Python?

A

Lowercase letters with underscores if needed

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

What are formal parameters in a function?

A

The parameters specified in the function definition

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

What are actual parameters (arguments) in a function?

A

The parameters specified when calling the function

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

How are immutable arguments passed to functions?

A

By call by value

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

How are mutable arguments passed to functions?

A

By call by reference (with value being the object reference)

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

What are positional arguments?

A

Arguments passed to a function based on their position, without explicitly naming parameters

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

What are keyword arguments?

A

Arguments passed to a function by explicitly naming parameters (e.g., param=value)

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

Can positional arguments appear after keyword arguments?

A

No

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

What does *args do in a function definition?

A

Collects arbitrary positional arguments into a tuple

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

What does **kwargs do in a function definition?

A

Collects arbitrary keyword arguments into a dictionary

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

What is parameter unpacking?

A

Using * to unpack iterables into positional arguments, or ** to unpack dictionaries into keyword arguments

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

How do you specify default parameter values?

A

In the function definition with an equals sign (e.g., def func(param=default_value))

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

What’s important to know about mutable default parameters?

A

They are evaluated once at function definition and can be changed across calls

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

What is type hinting in Python?

A

Optional hints about parameter and return value types (e.g., def func(x: int) -> int)

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

What happens if a function doesn’t explicitly return anything?

A

It returns None by default

17
Q

What is a namespace in Python?

A

A dictionary that maps names (variables functions etc.) to their objects

18
Q

What are the different types of namespaces in Python?

A

Built-in namespace, global/module namespace, enclosing/nested namespaces, and local namespace

19
Q

What is the scope lookup order in Python?

A

Local namespace, then enclosing namespaces, then global namespace, then built-in namespace

20
Q

What’s in the built-in namespace?

A

Python built-in functions and objects (like len dict print etc.)

21
Q

What’s in the global namespace?

A

Names defined in the main script/module (like global variables)

22
Q

What’s in the local namespace?

A

Names defined at the innermost level (like local variables within a function)

23
Q

What happens when a return statement is encountered?

A

The function immediately terminates and returns the specified value

24
Q

Can you mix normal parameters with *args and **kwargs?

A

Yes, with specific rules about order and keyword-only arguments