-1
def quick_sort(array, start, end):
    if start >= end:
        return
    p = partition(array, start, end)
    quick_sort(array, start, p-1)
    quick_sort(array, p+1, end)

I have tried this code (a fragment of quick sort) in python.

Can the return statement be used as such without returning any value?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 2
    try running this and see what happens: `def f(): return`. (spoiler: it will return `None`), – hiro protagonist Feb 22 '20 at 14:40
  • 1
    `return` with no value is equivalent to `return None`. If you reach the end of a function without encountering a `return` statement at all, that's the same as having an explicit `return`. – chepner Feb 22 '20 at 14:43

3 Answers3

0

It is possible to do so. What will happen is that python will simply return None.

For more information: link

afterm3
  • 69
  • 1
  • 5
0

Yes, return is the same as return None.

Also, having no return statement is the same as return None. You could replace your code with:

def quick_sort(array, start, end):
    if start < end:
        p = partition(array, start, end)
        quick_sort(array, start, p-1)
        quick_sort(array, p+1, end)
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
0

Python is a language and every language has a set of rules, so python said:

"if you must define a function, and you intend to use that function to communicate with the other programs, use return."

A return statement is used to end the execution of the function call and “returns” the result (value of the expression following the return keyword) to the caller. If the return statement is without any expression, then the special value None is returned.

Princely
  • 61
  • 1
  • 8