Because the exercise requires you to do so. I can understand your confusion, you want to ask:
Question: We didn't have the
return statement in the previous exercise. Why do we have to insert the
return statement in this exercise then?
On the previous exercise, we had this code:
arr.sort! is the last statement of the method
alphabetize, it also becomes the return value of that method. Hence, even though you don't have an explicit
return statement, the method will still return a value.
Then, you modify the code to:
number = [1,3,2,1,3,4]
(Note that, here,
arr.sort! is not the last statement of the code, it is
So, when you do:
alphabetize(number, true) the method executes till the last statement and returns a value. However, when you do
alphabetize(number) only, the
if rev is
false and so the last statement is not executed. And therefore, no value is
But the code expects you to return a value, therefore, you need to add an explicit
return statement at the end like this:
to make your code complete and correct.
I hope it helps!