INFO1110 Lecture Notes - Lecture 5: Order Of Merit, Boolean Expression
Document Summary
Why do we need to use functions: organizes code into logical group so it is tidy and easy to understand, code can be re-used, no need to repeat code this reduces error. Examples def add(a, b): return a + b result = add(1, 4) print(result) 5 def is_even(n): if n % 2 == 0: return true return false print(is_even(4)) print(is_even(5)) print(is_even(6)) # or another way of writing the above. # because (n % 2 == 0) is a boolean expression def is_even(n): return n % 2 == 0 print(is_even(4)) print(is_even(5)) print(is_even(6)) There are two ways a function can end: if the function returns using the return keyword, if the function raises an error.