Functions Flashcards

1
Q

What is decomposition?

A

Different pieces of self-contained code work together towards a goal

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

What is abstraction?

A

Suppressing details of method to compute something as advertised

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

What are the characteristics of a function?

A

Keyword, Name, Parameters(optional), Docstring, Body

def is_even(i):
“””
Optional documentation describing function
“””
return(i)
#A function must be invoked to run: is_even(3)

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

What must be used to get a value when using a function?

A

return

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

What is global and function scope?

A
Global scope is the scope of bound values
Function scope is values that are bound within the function, this does not affect global scope
#Variables assigned values will stay within scope unless referenced in the parameter of another scope
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference between return and print?

A

Return is used only in function
Only one return is executed in a function, after it breaks from the function
Has a value associated from the function invoking it

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

What is an internal/helper function?

A

A function that is evaluated (made) within an existing function

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

What is important when invoking a function?

A

Any parameters must also be invoked likewise

Evaluate: def func_a() Invoke: func_a()
Evaluate: def func_b(x,y,z) Invoke: func_b(3,2,4)

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

How do you give a parameter in a function a default value?

A

Bind it
Evaluate: def func_a(a = True, b = False)
if a:
print(“a is true”)
Invoke: func_a() #Parameter does not have to be specified

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

What is best practice for the function docstring?

A

Assumptions: conditions that must be met by clients of the function; typically constraints on values of parameters
Guarantees: conditions that must be met by function, providing it has been called in a manner consistent with assumptions

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

How do you use methods as functions?

A

Dot notation

s.capitalize

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

What are some built in python methods on strings?

A
.upper()
.capitalize()
.isupper()
.islower()
.swapcase()
.index('i')
.find('i')
.count('i')
.replace('i','e')
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is recursion?

A

Recursion is a way of coding a problem in which function calls itself within its own body

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