Defining Functions

To define your own function, use the def keyword, followed by the function name, which must follow the same rules for variable names. After the function name, put a pair of parentheses with variable names inside the parentheses. These variables are called parameters, and the values are passed into the function when using it. Example of a function that can add any two numbers:
def sum(a, b):
  result = a + b
Make sure to indent each line in the body of the function. If you try to use this function and print the result, you will get an error:
def sum(a, b):
  result = a + b

sum(5, 9)
print(result)

# Traceback (most recent call last):
#   File "main.py", line 5, in <module>
#     print(result)
# NameError: name 'result' is not defined
To access the value of the result variable, the function has to return the value using the return keyword. Example of returning the value and saving it in a variable:
def sum(a, b):
  result = a + b
  return result

total = sum(5, 9)
print(total)
# 14

Exercise 1 of 4

Finish the function.
def multiply(a, b):
  result = a * b
  # return the value of result here

print(multiply(4, 2))

Your code

Exercise 2 of 4

Finish the function.
def subtract(a, b):
  # find the value of a - b so you can return it
  return result

print(subtract(4, 2))

Your code

Exercise 3 of 4

Finish the function.
def power(a, b):
  # find the value of a raised to the power of b
  # return the result

print(power(4, 2))

Your code

Exercise 4 of 4

Write a function called divide to divide one number by another number.
print(divide(4, 2))

Your code