Ch.8 Functions Flashcards
What is a function?
A named block of code that is designed to do one specific job, very very well.
Where do the parameters and arguments go in your code?
def function_name(parameter) ................
function_name(argument)
What is the argument?
It is a piece of information that’s passed form a function call to a function.
How do you use a default value?
def funciton_name( parameter_1, parameter_2 = default value)
What does a return statement do?
It takes a value from inside a function and sends it back to the line that called the function.
Where do you place the parameter if the argument if it is optional?
You place it last, for example: def function_name(p_1, p_2, optional_p)
How do you modify a list?
By making changes to the list inside the function’s body, because this change is permanent.
How do you prevent a function from modifying a list?
By sending a copy of a list to a function, like this: function_name(list_name[:])
because the slice notation [:] makes a copy of the list.
How do you pass an arbitrary number of arguments?
by using an asterisk: def function_name(*parameters)
the asterisk tells python to make an empty tuple and pack whatever values it receives into the tuple.
How do you mix positional arguments and arbitrary arguments?
def function_name(positional, *arbitrary)
the arbitrary one goes last
How do you use arbitrary keyword arguments?
With two asterisks before the parameter.