'Why does this implementation of quicksort return from a void function?

#include<iostream>
using namespace std;

template <class Item>

void quicksort(Item a[], int l, int r)
{
    if (r <= 1) return;

    int i = partition(a, l, r);
    quicksort(a, l,   i-1);
    quicksort(a, i+1, r);
}

This program is taken from Algorithms in C++ by Robert Sedgewick. I have a one confusion in this program. We are using a function which have void return type. and we are using return. What does return do in this program if it will not return any value?



Solution 1:[1]

The return "returns" from the function to the calling function if r is less than or equal to 1. It's basically telling you that it's pointless to continue if r is not 2 or greater.

See also If void() does not return a value, why do we use it?

Solution 2:[2]

If you do not specify any value after return, it means return void.

return; 

won't return anything, which matches the declared void return type of the quicksort function. It also means that it will return to the caller of the quicksort function in this case.

Solution 3:[3]

A return statement in a void function immediately exits the function. Here, the return statement is used so that if an array of size 0 or size 1 is to be sorted, then the function does nothing to it and does not continue to recurse. This function could also have been written using an if statement, as follows:

template <class Item> void quicksort(Item a[], int l, int r) {
    if (r > 2) {
        int i = partition(a, l, r);
        quicksort(a, l,   i-1);
        quicksort(a, i+1, r);
    }
}

This function has the same meaning as your original version of the function, but does not explicitly contain a return statement.

Hope this helps!

Solution 4:[4]

The return part is just an early termination command. It stops the function if r<=1 is true, and prevents the rest of the code from running. The point with this code is that you don't need to sort if you have one or zero elements.

Solution 5:[5]

In your example return is just used to get out of the function i.e. to return to where it was called.

Solution 6:[6]

You use return in order to not continue executing the rest of the function.

As its name tells.. it returns from the function.

Code after it has returned doesn't run.

(Read the other 100 answers and you'll never forget this).

Solution 7:[7]

It's not actually returning a value. all that return statement does is terminate the function

you don't want to

int i=partition(a,l,r);
qucksort(a,l,i-1);
qucksort(a,i+1,r);

if

if(r<=1)

on a related note, I personally think it's better form to

if(r>1)
{
    int i=partition(a,l,r);
    qucksort(a,l,i-1);
    qucksort(a,i+1,r);
}

Solution 8:[8]

It's just used to end the function at that point, if the condition is met.

Solution 9:[9]

A return statement in a function that has a void return type simply exits from the function and returns to its caller. The flow of control then continues from the next instruction or expression evaluation.

Also notice, that flowing off the end of a void-returning function is equivalent to a return statement such as:

return;

Solution 10:[10]

See this answer: Can I return in void function?

In this specific case, my guess is that the author intended this to stop further execution of the function.

Solution 11:[11]

That version of quicksort will sort the input array in-place (as opposed to returning a sorted array). The line if (r <= 1) return; will return from the function early if the array has one or fewer elements, skipping the rest of the statements in the function.

So, it doesn't return any value, it just returns control to where it was called from without doing anything to the input.