02-Functions 4pp
02-Functions 4pp
02-Functions 4pp
A precise sequence of simple steps to solve a problem translating an algorithm into a computer program
Type: int Type: oating-point
meaning_of_life = 42
a = 6.02
print( meaning_of_life )
output: 42
fl
Type: string Type: string
last_letter = "z" print( "hello" )
output: z
hello = 5
print( hello )
output: 5
print( int(3.14) ) 3
x = True # not same as x = “True”
a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
d = "c: " + str(c) d = "c: " + str(c) c: 8
b = 30
a = b
fl
a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
d = "c: " + str(c) c: 8 d = "c: " + str(c) c: 8
b = 30 b = 30
a = b a —> 30 a = b a —> 30
4 = a
a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
d = "c: " + str(c) c: 8 c = "hello"
print( b + c )
b = 30
a = b a —> 30
print(e) error
4 = a error
a = 5 Practice a = 5 Practice
b = 3 b = 3
c = a + b c = a + b
c = "hello" c = "hello"
print( b + c ) error print( b + c ) error
# the function len takes as input a string and returns an integer y = 24 / compute_four() 6
x = len("eggplant")
print( compute_four() ) 4
# the return value of one function can be the input to another
x = int(8.485) + 12
x = int(sqrt(72)) + 12
x = add_five(z)
print(x) 9
print(add_five(z)) 9
Passing & Returning
def return_two_things(x,y):
return(x+y,x*y)
print(x,y)
(s,p) = return_two_things(2,5)