Functions Flashcards
What is decomposition?
Different pieces of self-contained code work together towards a goal
What is abstraction?
Suppressing details of method to compute something as advertised
What are the characteristics of a function?
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)
What must be used to get a value when using a function?
return
What is global and function scope?
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
What is the difference between return and print?
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
What is an internal/helper function?
A function that is evaluated (made) within an existing function
What is important when invoking a function?
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 do you give a parameter in a function a default value?
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
What is best practice for the function docstring?
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 do you use methods as functions?
Dot notation
s.capitalize
What are some built in python methods on strings?
.upper() .capitalize() .isupper() .islower() .swapcase() .index('i') .find('i') .count('i') .replace('i','e')
What is recursion?
Recursion is a way of coding a problem in which function calls itself within its own body