Logic functions
Often programmers use boolean functions that return boolean values "true" or "false" (
trueor
false)
Such functions are useful for check some property.
Let's consider two examples of writing a logical function that checks if a number is even.
Description |
Subroutine example |
1) Better way: the result of the expression n % 2 == 0 will be true (True) or false (False) |
def isEven(n):
return (n % 2 == 0)
|
2) You can write it like that, but it’s better not to do a longer record anyway |
def isEven(n):
if n % 2 == 0:
return true;
else:
return False
|
And the last note about working with functions and procedures: the number of functions and procedures in the program is not limited. In addition, one subroutine can call another subroutine and even itself.