4
#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?

4
  • 2
    Let the answers flow :)
    – stardust
    Apr 29, 2013 at 16:59
  • 1
    You get an upvote, You get an upvote, Everyone gets an upvote :P
    – stardust
    Apr 29, 2013 at 17:01
  • @Named Yes, an interesting observance of evolution of the question and answers (but apparently no duplicate) ... Apr 29, 2013 at 17:46
  • Thanks alot guyz. Really a great website
    – Saad Ullah
    Apr 30, 2013 at 1:59

11 Answers 11

3

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?

3

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.

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!

2

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.

2

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

2

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).

0
1

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);
}
1

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

1

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;
1

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.