Lesson Notes
Functions are stored algorithms. Algorithms operate on data.
Without data, an algorithm is just a set of instructions.
When we give data to a function, the data is called an argument.
print("Hello")
"Hello"
is the argumentrange(1,20,2)
1
,20
,2
are the argumentsWhen we write a function we are trying to solve a single problem. This is known as the Single Responsibility Principle.
Single Responsibility Principle: An algorithm or data object should be responsible for one, and only one, thing.
Given the SRP, why is using input
in a function considered bad practice?
Consider that Data sources change over time. Algorithms do not. A function should not be responsible for both aquiring data and processing data.
Always get data outside of a function, then pass those values in as arguments.
Parameters define the type of data a function needs
The input()
function takes a str, any str, as a parameter
val = input("This is valid data string")
val = input("50 15 7415")
As long as the data type supports the operations, it doesn't matter what the data value is:
import turtle
def foo(a): # a is parameter - any valid int for this function
print(abs(a+a)) # int supports the `+` operation
x = -5
foo(x) # x is the argument - it holds the literal value -5
A function must declare what parameters it can take.
def foo():
print("nothing")
def bar(x, y):
print(x, y)
foo(5) # !ERROR: error because foo declares no parameters
bar( 'a', 'b', 'c') # !ERROR: because bar only declares 2 parameters
Parameters define:
Define parameters inside the parenthesis using the same naming conventions as variables:
arg1, arg2
When you pass an argument to a function, only the data value gets passed. The parameter name is an entirely new name for that data.
IMPORTANT: Parameter names have nothing to do with the name of the argument being passed.
We can assign a default value to a parameter to make it optional:
def funct_with_default_params(arg1=0, arg2=””):
print(arg1, arg2)
funct_with_default_params(arg1=5) # arg2 will be set to the default ""
Providing a default value, also called a named parameter, also lets you pass arguments in any order
Positonal Parameters | Named Parameters |
---|---|
arguments required | arguments optional |
must be passed in correct order | can be passed in any order |
def funct_with_default_params(arg1=0, arg2=0):
print(arg1-arg2)
def funct_with_positional_params(arg1, arg2):
print(arg1-arg2)
funct_with_default_params(arg1=5, arg2=3) # will always result in 2
funct_with_default_params(arg2=3, arg1=5) # will always result in 2
funct_with_positional_params(5, 3) # will also result in 2
funct_with_positional_params(3, 5) # will result in -2
You can use both named and positional parameters, but named parameters must come after positional parameters
def funct_with_mixed_params(arg1,arg3=0, arg4=””, arg2): # ERROR! default params must come last
print(arg1, arg2, arg3, arg4)
def funct_with_mixed_params(arg1, arg2, arg3=0, arg4=””):
print(arg1, arg2, arg3, arg4)
So IMPORTANT it needs repeating:
Function parameter names have nothing to do with the variable name being passed as an argument
Try running the code below for yourself, and make sure you can predict and understand the printed the results.
foo(x):
x = 5
x = 10
foo(x)
print(x)
All local variables are deleted at the end of the function
#num and double_num are created just for this function
def myFunc(num=0):
double_num = num + num
print(num) # WORKS!
print(double_num) # WORKS!
myFunc(num=5)
#after calling this function num, and double num are deleted from memory
print(num) # ERROR!
print(double_num) # ERROR!
Local variables can only be accessed inside the function
You cannot access a local variable outside of a function because it does not exist
(remember, it was deleted as soon as the function completes)
Where you can use a variable is known as the variable's SCOPE
def my_min(x,y,z):
result = x
if result > y:
result = y
if result > z:
result = z
print(result)
result = 0
my_min(1,2,3)
print(result)