Chapter 6 (functions)
How do you define a function?
def functionName(parameter, parameter):
code
code
What is a docstring?
def functionName():
''' this is a docstring '''
codeDoc strings are like commments in which you can describe what the function is doing
How to call a function
functionName(argument, argument)
What does the return statement do?
This breaks the function and will return any value that is given after the return statment
return value, value
What is abstraction?
This is the idea that a variable outside of its scope cannot be accessed
What is a positional argument?
This is when the position of the argument is assigended to the same position parameter
What is a keyword argument?
This is when you set a keyword, so the parameter name to a value
functionName(parameterName = 'hello')
Can you have positional aruments after keyword arguments?
NO
valid:
functionName(argument, argument, parameterName = 'wow')
functionName(argument, parameterName = 'yo', parameterName = 'wow')
invalid:
functionName(argument, parameterName = 'wow', argument)
How do you define default values?
def functionName(parameter = 'hello')
Note: once you have defined a default value to one parameter you must to all proceeding it
Valid:
def functionName(parameter, parameter = 'yo', parameter = 'bang')
Invalid:
def functionName(parameter = 'do not', parameter)
What are the two types of scope for variables called?
- local variables
- global variables
Can you change global variables in a local scope?
yes
How?
global variableName
This grants you full access to the variable
What is shadowing a variable mean?
This is when you create a new variable in a local scope with the same name as one in the global scope, so that you can pretend to manipulate the original variable.
This will not change the value of the global variable.
Lambda
funcName lambda agrument : expression
funcName lambda a, b : a + b
#returns a + b
# harder method
def func(n):
return lambda a : a * n
widerFunc = func(100000)
print(widerFunc(1.1))
#prints a * b