Expedia Code Academy
Functions are bits of code that are reusable. This is handy because:
# The print function with 1 argument: a string
print("hello")
# The len function with 1 argument: a list
len([0, 1, 2, 3])
The syntax involves these key words: def and return
Here's a function that adds 2 number arguments together:
def add_two_numbers(num1, num2):
result = num1 + num2
return result
# Let's call our function!
sum = add_two_numbers(612, 847)
Always write your function before you call it!
The below won't work - what error do you get?
# Let's call our function!
sum = add_two_numbers(612, 847)
def add_two_numbers(num1, num2):
result = num1 + num2
return result
sum = add_two_numbers(612, 847, 555)
What will sum be in this example?
def add_two_numbers(num1, num2):
result = num1 + num2
return result
sum = add_two_numbers('my hovercraft is ', 'full of eels')