The Python Programming Language: Functions
1 2 3 |
x = 1 y = 2 x + y |
add_numbers is a function that takes two numbers and adds them together.
1 2 3 4 |
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.
1 2 3 4 5 6 7 8 |
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.
1 2 3 4 5 6 7 8 9 |
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.
1 2 3 4 5 |
def add_numbers(x,y): return x+y a = add_numbers a(1,2) |