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
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
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
def multiply(a, b): result = a * b # return the value of result here print(multiply(4, 2))
Your code
def subtract(a, b): # find the value of a - b so you can return it return result print(subtract(4, 2))
Your code
def power(a, b): # find the value of a raised to the power of b # return the result print(power(4, 2))
Your code
print(divide(4, 2))
Your code