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,750 points
43 views

2 answers to this question.

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,520 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
• 13,160 points
70 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
45 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,520 points

edited Dec 11, 2018 by Nymeria 630 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
57 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 232 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,520 points
117 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
57 views