Functions Flashcards
what is a function and what keyword is used to create one?
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.
Function parameters vs arguments
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.
Can you call a function before it ‘s declaration?
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.
What value is returned by a function without a return keyword or the return keyword without a value?
None
Can a Python function return multiple values?
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.
What are the three types of arguments we can pass to a function?
Positional arguments, Keyword arguments, and Default arguments
What are positional arguments?
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)
What are Keyword arguments?
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)
What are default arguments?
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 would you create a function that accepts unlimited arguments?
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.
Accept unlimited keyword arguments?
Note kwargs is just a convention and not a keyword.
Unlimited args and kwargs
args:tuple kwargs:dict
Positional args only?
Kwargs only?
Combination args?