Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
return statement usage
#1
Hello!

1)
def square(x):
    return(x*x)
print(square(4))

2)
def square(x):
    print(x*x)
square(4)
Why do I have to use a return statement(1) when I can get the output by calling the function directly(2)?
Reply
#2
Maybe you should start with question - why you need function for printing something out Smile

Functions with no return or yield return None:

>>> def square(x):
...     print(x*x)
... 
>>> square(4)
16
>>> print(square(4))
16
None      
>>> a = square(4)
16
>>> a
>>> a is None
True
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
#3
Okay. Thank you :-)
Reply
#4
(Jul-16-2019, 09:12 AM)SB_J Wrote: Why do I have to use a return statement(1) when I can get the output by calling the function directly(2)?
Functions is a first step to structure code better,one function can do a task and return data out.
Then other functions can take that data in as argument and do other task.
This make code easier to read and test(functions should be isolate from data outside) than writing all code in global namespace.

A working example of what mention over.
from bs4 import BeautifulSoup
import json
import requests

def data_from_web():
    '''Read title from web-site'''
    url = 'http://CNN.com'
    response = requests.get(url)
    soup = BeautifulSoup(response.content, 'html.parser')
    return soup.find('title').text

def process_data(data):
    '''Make dicionary'''
    modified_data = {'title': data}
    return modified_data

def write_data(data):
    '''Serializing data to disk'''
    with open('title.json', 'w') as f_out:
        json.dump(data, f_out)

def result():
    data = data_from_web()
    modified_data = process_data(data)
    write_data(modified_data)

if __name__ == "__main__":
    result()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  yield usage as statement or expression akbarza 5 793 Oct-23-2023, 11:43 AM
Last Post: Gribouillis
  Need help with Return statement Columbo 13 2,243 Sep-17-2022, 04:03 PM
Last Post: Columbo
  How to invoke a function with return statement in list comprehension? maiya 4 2,814 Jul-17-2021, 04:30 PM
Last Post: maiya
  syntax error on return statement l_butler 5 3,070 May-31-2020, 02:26 PM
Last Post: pyzyx3qwerty
  return statement will not work TheTechRobo 2 2,607 Mar-30-2020, 06:22 PM
Last Post: TheTechRobo
  HELP! Return Statement Debugging HappyMan 5 3,102 Jan-27-2020, 07:31 PM
Last Post: michael1789
  Embedding return in a print statement Tapster 3 2,264 Oct-07-2019, 03:10 PM
Last Post: Tapster
  I don't understand this return statement 357mag 4 2,734 Jul-10-2019, 07:02 PM
Last Post: perfringo
  Return Statement in Python IDLE editor NMW 10 10,553 Jul-11-2017, 09:47 PM
Last Post: NMW
  Need help understanding return statement python_lover 4 4,357 Mar-03-2017, 10:09 AM
Last Post: python_lover

Forum Jump:

User Panel Messages

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