What is Return in Python

In Python return
is a statement used end the execution of the function call and “returns” the result (value of the expression following the return
keyword) to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the value None
is returned.
Syntax:
def fun():
statements
.
.
.
return [expression]
Example:
def square(n): if n < 0 : return else: return n*n re = square(5) print("The square of 5 is ", re) re = square(-1) print("The square of -1 is ", re) def color(c): if c == 'red': return "the color is red" cl = color('red') print(cl)
Output:
The square of 5 is 25 The square of -1 is None the color is red
A return statement can not be used outside a function. If used outside a function, it will raise a SyntaxError.
>>> def sq(n): ... return n*n >>> sq(4) 16 >>> return sq(4) File "<stdin>", line 1 SyntaxError: 'return' outside function >>>
Subscribe
Login
Please login to comment
0 Discussion