The Python Programming Language: Functions
In [1]:
Out[1]:
In [2]:
In [3]:
In [4]:
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)
3
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))
3 6
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))
Flag is true! 3
add_numbers to variable a.def add_numbers(x,y):
return x+y
a = add_numbers
a(1,2)
3
Comments