Functions Flashcards

1
Q

what is a function ?

A

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.

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

what is the - def - keyword

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

how to set up the start of a function

A

def function_name():

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

what is calling a function

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you call a function

A

To call our function, we must type out the function’s name followed by a pair of parentheses and no indentation:

function_name()

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

How is indentation in functions important for execution flow

A

In Python, the amount of whitespace tells the computer what is part of a function and what is not part of that function.

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

what are function paramaters

A

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)

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

what is a paramater and what does it do ?

A

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

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

what is an argument

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

how do we actually use a parameter?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to set mutiple paramaters

A

We can write a function that takes in more than one parameter by using commas:

def my_function(parameter1, parameter2, parameter3):

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

How to set an argument with mutiple paramaters

A

my_function(argument1, argument2)

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

what are Positional arguments

A

arguments that can be called by their position in the function definition.

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

How do you set keyword arguments

A

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)

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

what are Keyword arguments

A

arguments that can be called by their name.

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

what are Default arguments

A

arguments that are given default values.

14
Q

How do you set Default arguments

A

want to give our function parameters default values. We can provide a default value to a parameter by using the assignment operator (=).
This will happen in the function declaration rather than the function call.

example: default value will be 10

def calculate_taxi_price(miles_to_travel, rate, discount = 10):

calculate_taxi_price(10, 0.5)

15
Q

What are user defined functions

A

User Defined Functions - functions that are written by users (like us!).

16
Q

What are built in functions

A

functions that come built into Python for us to use

17
Q

What does help() function do

A

will print a link to documentation for us and provide some details

17
Q

What does the len() built in function do

A

To get the length of a string

length_of_destination = len(destination_name)

18
Q

what does the max() and min() function do

A

max = Return the largest item in an iterable or the largest of two or more arguments.

min = returns the smallest value in a varible

19
Q

what does the round() function do

A

values are rounded to the closest multiple of 10 to the power minus ndigits

20
Q

How to set the scope of a variable inside and outside of a function

A

This function will print the favorite locations

favorite_locations = “Paris, Norway, Iceland”

def print_count_locations():
print(“There are 3 locations”)

def show_favorite_locations():
print(“Your favorite locations are: “ + favorite_locations)

print_count_locations()
show_favorite_locations()

21
Q

what does the return keyword do

A

Functions can also return a value to the program so that this value can be modified or used later

allows us to reuse the value (in the form of a variable) throughout the rest of the program.

22
Q

How to you return mutiple values

A

weather_data = [‘Sunny’, ‘Sunny’, ‘Cloudy’, ‘Raining’, ‘Snowing’]

def threeday_weather_report(weather):
first_day = “ Tomorrow the weather will be “ + weather[0]
second_day = “ The following day it will be “ + weather[1]
third_day = “ Two days from now it will be “ + weather[2]
return first_day, second_day, third_day

This function takes in a set of data in the form of a list for the upcoming week’s weather. We can get our returned function values by assigning them to variables when we call the function:

monday, tuesday, wednesday = threeday_weather_report(weather_data)

print(monday)
print(tuesday)
print(wednesday)

23
Q
A
23
Q
A
24
Q
A
24
Q
A