The Python Programming Language: Functions
x = 1 y = 2 x + y
add_numbers is a function that takes two numbers and adds them together.
def add_numbers(x, y):
return x + y
add_numbers(1, 2)
add_numbers updated to take an optional 3rd parameter. Using `print` allows printing of multiple expressions within a single cell.
def add_numbers(x,y,z=None):
if (z==None):
return x+y
else:
return x+y+z
print(add_numbers(1, 2))
print(add_numbers(1, 2, 3))
add_numbers updated to take an optional flag parameter.
def add_numbers(x, y, z=None, flag=False):
if (flag):
print('Flag is true!')
if (z==None):
return x + y
else:
return x + y + z
print(add_numbers(1, 2, flag=True))
Assign function add_numbers to variable a.
def add_numbers(x,y):
return x+y
a = add_numbers
a(1,2)