Search code examples
pythonreturnuser-defined-functionspython-3.6

Some questions about the following code


I have the following questions about the following code:

  1. What value does 0 hold in the second line? Is it something like 'true' or 'false'? Or a numerical value?

  2. Are the return statements necessary in the user_even function? The code works without them but it seems that all user-defined functions have a return statement in them or am I wrong?

def divisible(num1, num2):
    return num1 % num2 == 0

def user_even():
    num1 = int(input ("Choose a number: "))
    num2 = int(2)

    if divisible(num1, num2): 
        print ("It's even")
        return
    else:
        print ("it's odd")
        return
user_even() 

Solution

  • For question 1, it evaluates the statement and returns a Boolean (True or False) value. The 0 is 0.

    return 5 % 5 == 0 # Remainder of 5/5 is 0 so that returns True
    return 5 % 4 == 0 # Remainder of 5/4 is 1 so that returns False
    

    For question 2, the return statements are not needed. A return statement should be used for variables or pieces of data that need to be returned from the function. In the code you provided, there is no data being returned so there is no need for the return statement.