If you have $100 saved in your bank account, and they pay you 3% interest every year, how much money will you have for each year, up to 10 years?
Hint: Use a for-loop. Don't forget to add the interest to the total
def function(parameter):
# block of code
def printName(name):
print("Hello, %s!" % name)
You define (make) the function like this:
And call it like this:
printName("John")
Which will return:
Hello, John!
Remember input()?
def savings(chores, paper, spending):
return chores + paper - spending
# or return(chores +paper - sending)
Another example, except with return() instead of print:
And call it like this:
print(savings(10, 10, 5))
Which will return:
15
What does return() do? What's the different between return() and print()?
# comments the code
# This is a global variable
a = 0
if a == 0:
# This is still a global variable
b = 1
def my_function(c):
# this is a local variable
d = 3
print(c)
print(d)
# Now we call the function, passing the value 7
# as the first and only parameter
my_function(7)
# a and b still exist
print(a)
print(b)
# c and d don't exist anymore -- these statements
# will give us name errors!
print(c)
print(d)