Python Forum
Need of return in function if statement inside the function already returns
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need of return in function if statement inside the function already returns
#1
Hello,

I'm learning python from some online tutorials. When learning about function, there was an exercise to check "Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False"

Before making function, i did following

a = (10, 20) 
sum(a) == 20 or a[0] == 20 or a[1] == 20
and it returns True if conditions satisfy.

but when I tried to make this in to a function

def func(a):
    sum(a) == 20 or a[0] == 20 or a[1] == 20
when called the above function
func((10,20))
. It didn't return anything. I had to put return inside the function to get True or False.

What is the need of this return since
sum(a) == 20 or a[0] == 20 or a[1] == 20
this line can already return true or False on it's own???
Thanks
Reply
#2
You forget the return statement:

def func(a):
    return sum(a) == 20 or a[0] == 20 or a[1] == 20
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Yeah I know that. What I meant is
When I run below code
a = (10, 20) 
sum(a) == 20 or a[0] == 20 or a[1] == 20
It gives the answer as
True
or
False
That means it returns something.
SO when I make this as a function, why do I have to type return before
sum(a) == 20 or a[0] == 20 or a[1] == 20
this, since it itself is returning the answer
Reply
#4
A function always returns None if something else isn't specified.
So, your function returns None. That is why you have to use the return statement in front of the expression to get the result.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
Also, when you enter something in the interactive interpreter, it evaluates it. It's not returning anything, it's evaluating the expression you entered.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Athul, what I have just recently learned is that the interpreter is only being nice to you by printing to the screen what you want to see, you are seeing what has happened within the interpreter and it is only showing you the internal answer, the evaluation only, and that evaluation is on stand by where it could be used by another part of the program if needed, but this is not the external answer that you need, this is why the return statement is actually needed, to give you the answer outside of the program where you can actually use it properly. I hope that explains it a little better.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Use of function/return Paulman 6 2,306 Oct-24-2021, 11:07 PM
Last Post: Paulman
  Multiple return from function Grimmar 7 3,490 Mar-22-2021, 09:20 PM
Last Post: Grimmar
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,485 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Lambda function not return value mbilalshafiq 4 3,252 Jul-04-2020, 07:47 AM
Last Post: ndc85430
  Child class function of Log return "None" mbilalshafiq 2 2,180 Jun-30-2020, 07:22 PM
Last Post: mbilalshafiq
  Question on "define function"; difference between return and print extricate 10 4,598 Jun-09-2020, 08:56 PM
Last Post: jefsummers
  [split] problem with function return value ops 1 3,275 Apr-13-2020, 01:48 PM
Last Post: buran
  Function to return today's month, day, and year sbabu 9 4,828 Jan-28-2020, 06:20 PM
Last Post: snippsat
  Passing a function to another function yulfmd1802 3 2,188 Jan-23-2020, 09:13 AM
Last Post: ndc85430
  Adding a sqrt function syntax error and <build-in function sqrt> from tkinter import Kael 0 1,826 Oct-14-2019, 05:51 PM
Last Post: Kael

Forum Jump:

User Panel Messages

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