Functions Flashcards
what is a function ?
Functions are a convenient way to group our code into reusable blocks. A function contains a sequence of steps that can be performed repeatedly throughout a program without having to repeat the process of writing the same code again.
what is the - def - keyword
The def keyword indicates the beginning of a function (also known as a function header). The function header is followed by a name in snake_case format that describes the task the function performs
how to set up the start of a function
def function_name():
what is calling a function
The process of executing the code inside the body of a function is known as calling it (This is also known as “executing a function”)
calling a function will print the print statements
How do you call a function
To call our function, we must type out the function’s name followed by a pair of parentheses and no indentation:
function_name()
How is indentation in functions important for execution flow
In Python, the amount of whitespace tells the computer what is part of a function and what is not part of that function.
what are function paramaters
Function parameters allow our function to accept data as an input value. We list the parameters a function takes as input between the parentheses of a function ( ).
e.g
def my_function(single_parameter)
what is a paramater and what does it do ?
A paramater is treated like a variable
def trip_welcome(destination):
print(“Welcome to Tripcademy!”)
print(“Looks like you’re going to “ + destination + “ today.”)
for the paramater we are telling it that it should expect some data to pass through
what is an argument
The argument is the data that is passed in when we call the function, which is then assigned to the parameter name.
def trip_welcome(destination):
print(“Looks like you’re going to “ + destination + “ today.”)
trip_welcome(“Times Square”)
how do we actually use a parameter?
def trip_welcome(destination):
print(“Welcome to Tripcademy!”)
print(“Looks like you’re going to “ + destination + “ today.”)
Our parameter of destination is used by passing in an argument to the function when we call it.
trip_welcome(“Times Square”)
How to set mutiple paramaters
We can write a function that takes in more than one parameter by using commas:
def my_function(parameter1, parameter2, parameter3):
How to set an argument with mutiple paramaters
my_function(argument1, argument2)
what are Positional arguments
arguments that can be called by their position in the function definition.
How do you set keyword arguments
where we explicitly refer to what each argument is assigned to in the function call.
example:
calculate_taxi_price(rate=0.5, discount=10, miles_to_travel=100)
what are Keyword arguments
arguments that can be called by their name.