Why do we use return statement in python?

0 votes
What is the simple basic explanation of what the return statement is, how to use it in Python and how is it different to print statement?
Oct 22, 2018 in Python by findingbugs
• 4,730 points
19 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In python, we start defining a function with "def" and end the function with "return".

A function of variable x is denoted as f(x). What this function does? Suppose, this function adds 2 to x. So, f(x)=x+2

Now, the code of this function will be:

def A_function (x):
    return x + 2

After defining the function, you can use that for any variable and get result. Such as:

print A_function (2)
>>> 4

We could just write the code slightly differently, such as:

def A_function (x):
    y = x + 2
    return y
print A_function (2)

That would also give "4".

Now, we can even use this code:

def A_function (x):
    x = x + 2
    return x
print A_function (2)

That would also give 4. See, that the "x" beside return actually means (x+2), not x of "A_function(x)".

answered Oct 22, 2018 by Priyaj
• 56,100 points
0 votes

def maximum(x, y):
    if x > y:
    return x
    else:
   return y 

print maximum(2, 3)
output:3

return statement exit a function and return a value. for more detail of python and return statement.

answered Apr 4 by anonymous

Related Questions In Python

0 votes
1 answer

What does the return statement do in Python?

The print() function is use to write ...READ MORE

answered Oct 1, 2018 in Python by SDeb
• 9,380 points
23 views
0 votes
1 answer

How to use nested if else statement in python?

The working of nested if-else is similar ...READ MORE

answered Nov 19, 2018 in Python by Nabarupa
27 views
0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,500 points

edited Dec 11, 2018 by Nymeria 118 views
0 votes
1 answer

Do we have pointers in python like other programming languages?

Hello No, we don't have any kind of ...READ MORE

answered Mar 12 in Python by MrBoot
• 1,210 points
33 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
19 views
0 votes
1 answer

Why there is no do while loop in python

There is no do...while loop because there ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 56,100 points
57 views
+1 vote
2 answers

How to use the pass statement in Python

In Python programming, pass is a null statement. The ...READ MORE

answered Apr 5 in Python by anonymous
26 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.