Search code examples
c++sortingreturnquicksortvoid

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

  • 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?