Functions
Defining functions in Python: with and without parameters, with return values, and a few short utility examples.
A function without parameters, using local variables:
def who_am_i(): #this is a function without parameters
name = "Jake" #local variable
age = 32 #int
print("My name is " + name + " and I am " + str(age) + " years old.") #string
who_am_i()
Taking a single parameter:
def add_one_hundred(num):
print(num + 100)
add_one_hundred(50) #outputs 150
Taking two parameters:
def add(x, y):
print(x + y)
add(7, 7) #outputs 14
Returning a value so it can be stored and reused:
def multiply(x, y):
return x * y
fourty_nine = multiply(7, 7) #used as a stored variable to put into another function
print(fourty_nine)
def square_root(x):
print(x ** .5) #or 1/2
square_root(64) #outputs 8
A helper that just prints a new line:
def nl(): #new line
print('\n')
nl()