Functions Flashcards

1
Q

what is a function and what keyword is used to create one?

A

A function is a reusable block of code, that returns a result or performs an action. The ‘def’ keyword is used to declair a function.

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

Function parameters vs arguments

A

The function parameters are the inputs to the function at its definition, the arguments on the other hand are the inputs used when calling the function.

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

Can you call a function before it ‘s declaration?

A

You cannot call a function before you declare it. A common convention in many languages is to use a single main function which is then called at the end as the entry point for the program, this ensures all subsequent functions are called in the correct order.

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

What value is returned by a function without a return keyword or the return keyword without a value?

A

None

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

Can a Python function return multiple values?

A

Yes. A return statement followed by a list of comma seperated values will return each value. When the function is called an equal number of variables must be used in the order they were returned.

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

What are the three types of arguments we can pass to a function?

A

Positional arguments, Keyword arguments, and Default arguments

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

What are positional arguments?

A

A positional arguments is an arguments that can be called by their position in the function definition.

positional call to function:

calculate_taxi_price(100, 10, 5)

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

What are Keyword arguments?

A

Arguments that can be called by their name.

keyword argument call to function:

calculate_taxi_price(rate=0.5, discount=10, miles_to_travel=100)

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

What are default arguments?

A

Arguments that are given default values in the function definition.

def calculate_taxi_price(miles_to_travel, rate, discount = 10):
  print(miles_to_travel * rate - discount )

When using a default argument, we can either choose to call the function without providing a value for a discount (and thus our function will use the default value assigned) or overwrite the default argument by providing our own.

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

How would you create a function that accepts unlimited arguments?

A

Note that to assing a value to the age argument you must use key-word assignment syntax (age=10) or position age as the first argument in the sequence.

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

Accept unlimited keyword arguments?

A

Note kwargs is just a convention and not a keyword.

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

Unlimited args and kwargs

A

args:tuple kwargs:dict

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

Positional args only?

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

Kwargs only?

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

Combination args?

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