INFO1110 Lecture Notes - Lecture 5: Order Of Merit, Boolean Expression

6 views1 pages

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.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents