Functions Flashcards

1
Q

To begin grouping related code, we start defining a function with the keyword…

A

def

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

How do you build the beginning of a function?

A

def
then the function’s name in snake case
Followed by parentheses
Then :

def greet_user():

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

How do we call a function?

A

We call a function by coding it’s name followed by parentheses like greet_user()

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

How do we start grouping related code?

A

by adding the def keyword

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

How do we name a function?

A

Using snake case

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

What does the colon : do?

A

They mark the beginning of a function’s code block

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

Where does the code we want to group together go?

A

After the function definition, indented by two spaces

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

How do we run the code inside a function?

A

By calling it with it’s name and ()

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

How do we pass a value to a function?

A

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.

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

Why do we pass specific values to functions?

A

So we can use the same code for different values

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

Where do parameters go?

A

Parameters go inside the parentheses of the function we’re creating

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

What does a parameter do?

A

It is a variable that passes a value to a function

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

How do we pass a value to the function’s parameter?

A

We call the function with the value between parentheses.

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

To return something from a function we add what keyword?

A

Return

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

What types of values can functions return?

A

Any type like string, integer, float or boolean.

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

How do we return a value from a function?

A

By adding the return keyword followed by the value to return.

17
Q

What sign allows us to add multiple parameters?

A

Commas
,

18
Q

How many parameters can a function have?

A

As many as we’d like

19
Q

When calling a function, how do we pass values to the function when it has multiple parameters?

A

In the order that the parameters are placed in the function definition.

20
Q

Why is it important to give functions descriptive names?

A

To know at a glance what the functions does

21
Q

What do function names usually start with?

A

Verbs (action words)

22
Q

What are examples of verbs used to name functions?

A

Create
Compute
Calculate
Get

23
Q

What are some special case verbs that are used for boolean functions?

A

Is
Has
Can

24
Q

What does keeping the same function naming mean?

A

Using the same verb for functions that perform similar tasks

25
Q

When does a variable have a local scope?

A

When we create it inside of a function’s block

26
Q

What does it mean for a variable to have a local scope?

A

We can’t access or update it outside of the function where it was created

27
Q

What happens when we try to access a local scope variable outside of the function that created it?

A

We get an error

28
Q

Which variables have a global scope?

A

Variables created outside of a function’s block

29
Q

From where can we access a variable with a global scope?

A

From anywhere in the code

30
Q

What kind of scope do variables created inside conditionals and loops have?

A

Local scope

31
Q

When can we first access a variable, regardless of scope?

A

Only after we create it

32
Q

What does nesting a conditional inside a function mean?

A

Indenting the conditional so that it’s part of the function’s code block

33
Q

What happens when we use a function’s parameter as part of the condition?

A

Our code can make a decision based on different input values.

34
Q
A