Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

How determine if a number is even or odd using Recursive Inner Function

"""
We can refactor the function to use a recursive inner function that calls itself to
subtract 2 from the starting number until the number is less than 2, and then use
% (modulo operator) with 2 and compare the remainder with zero to return a
Boolean. Even numbers are divisible by 2 and don’t have a remainder.
"""

def is_even_recursive(number):
	def check(num):
		if (num < 2):
			return num % 2 == 0
		return check(num - 2)
	
    if check(number):
		return True
	return False
    
 
PREVIOUS NEXT
Tagged: #How #determine #number #odd #Recursive #Inner #Function
ADD COMMENT
Topic
Name
3+4 =