Functions Flashcards
To begin grouping related code, we start defining a function with the keyword…
def
How do you build the beginning of a function?
def
then the function’s name in snake case
Followed by parentheses
Then :
def greet_user():
How do we call a function?
We call a function by coding it’s name followed by parentheses like greet_user()
How do we start grouping related code?
by adding the def keyword
How do we name a function?
Using snake case
What does the colon : do?
They mark the beginning of a function’s code block
Where does the code we want to group together go?
After the function definition, indented by two spaces
How do we run the code inside a function?
By calling it with it’s name and ()
How do we pass a value to a function?
By adding a variable called a parameter inside the parentheses of the function.
def greet(name)
In this example, name is the variable called a parameter.
Why do we pass specific values to functions?
So we can use the same code for different values
Where do parameters go?
Parameters go inside the parentheses of the function we’re creating
What does a parameter do?
It is a variable that passes a value to a function
How do we pass a value to the function’s parameter?
We call the function with the value between parentheses.
To return something from a function we add what keyword?
Return
What types of values can functions return?
Any type like string, integer, float or boolean.
How do we return a value from a function?
By adding the return keyword followed by the value to return.
What sign allows us to add multiple parameters?
Commas
,
How many parameters can a function have?
As many as we’d like
When calling a function, how do we pass values to the function when it has multiple parameters?
In the order that the parameters are placed in the function definition.
Why is it important to give functions descriptive names?
To know at a glance what the functions does
What do function names usually start with?
Verbs (action words)
What are examples of verbs used to name functions?
Create
Compute
Calculate
Get
What are some special case verbs that are used for boolean functions?
Is
Has
Can
What does keeping the same function naming mean?
Using the same verb for functions that perform similar tasks