Python Forum
I don't understand this return statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I don't understand this return statement
#1
This book I'm working out of talks about making a boolean function. It works good and here is the code:
def is_even(number):
    if(number % 2 == 0):
        status = True
    else:
        status = False
    return status

number = int(input("Enter a number: "))
is_even(number)

if is_even(number):
    print("The number is even")
else:
    print("The number is odd")
What I don't understand is at the bottom of the function the line that reads return status. Where is status going? There is no variable that status is being assigned to.

And when I look at the is_even if statement structure there is no mention of status there either.

So I don't understand where status is going and how it's being checked in the if statement.
Reply
#2
status is the name of the variable assigned True or False by the if statement in the function definition. Return then returns the value of status to the calling function. This can then be assigned to a variable or used in an expression.

After the input statement, the function is called and nothing is done with the result.
In the if statement after that, the function is called a second time and the result is used as the condition for the if statement.

So, the line after the input statement is redundant. That may be your source of confusion.
Reply
#3
The line after the input statement is where we are calling the function. I don't see how that can be redundant. We need that line otherwise the function never gets called.

So return status returns either a True or False value to the line is_even(number). Seems strange how a result can be returned to a line and it never gets assigned to anything.

Unless status gets returned to number:

if is_even(number): really ends up being if is_even(False):
Reply
#4
(Jul-10-2019, 05:57 PM)357mag Wrote: The line after the input statement is where we are calling the function. I don't see how that can be redundant. We need that line otherwise the function never gets called.


no your calling that function twice.

This is your second call
Quote:
if is_even(number):

This function call is doing nothing and can be commented out (the second line here)
Quote:
number = int(input("Enter a number: "))
is_even(number)

IF you wanted to use that one you would use the return value like this
value = is_even(number)
...
if value:
The thing to remember is this piece of code gets replaced with the return value
is_even(10) -> True
Which in the if condition results to
if True: 
which executes the if condition

Where as this
is_even(11) -> False
which in the if condition results to
if False:
which does not execute the if condition and falls to the else clause.

So because your first call to is_even function here
Quote:
number = int(input("Enter a number: "))
is_even(number)
does not use the return value at all, there is no point to it being there.

It is the same as doing this
number = int(input("Enter a number: "))
True
Recommended Tutorials:
Reply
#5
Nothing to add to metulburr excellent explanation.

In programming same result can be achieved in different ways. Alternative approach without function and if-else, requires 3.6 <= Python:

num = int(input('Enter a number: '))
print(f"The number is {num % 2 and 'odd' or 'even'}")
If the objective is to print out whether number is odd or even one can skip function, if-else statement.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with Return statement Columbo 13 2,288 Sep-17-2022, 04:03 PM
Last Post: Columbo
  How to invoke a function with return statement in list comprehension? maiya 4 2,848 Jul-17-2021, 04:30 PM
Last Post: maiya
  .Set - Unable to understand the statement ateestructural 5 2,386 Aug-02-2020, 08:24 PM
Last Post: deanhystad
  Unable to understand a statement in an existing code ateestructural 1 2,230 Aug-01-2020, 09:38 PM
Last Post: deanhystad
  syntax error on return statement l_butler 5 3,114 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  return statement will not work TheTechRobo 2 2,635 Mar-30-2020, 06:22 PM
Last Post: TheTechRobo
  HELP! Return Statement Debugging HappyMan 5 3,127 Jan-27-2020, 07:31 PM
Last Post: michael1789
  Embedding return in a print statement Tapster 3 2,286 Oct-07-2019, 03:10 PM
Last Post: Tapster
  return statement usage SB_J 3 2,430 Jul-16-2019, 07:24 PM
Last Post: snippsat
  I don't understand the parameters in a statement that uses the sh library RedSkeleton007 1 2,641 Apr-11-2018, 07:55 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020