Why does a void function return a value?

前端 未结 5 2008
北海茫月
北海茫月 2021-01-27 07:54

I\'m a programming beginner and I have question regarding a return value from a function.

I´m studying Java.

I have attached code from my book that features a cl

5条回答
  •  梦如初夏
    2021-01-27 08:14

    The sort() method modifies the array passed as an argument; that is, the original array is modified.

    Say you have array a which value is [ 3, 2 ]; you call sort(a); if your code looks like:

    // printArray is an hypothetical method
    printArray(a);
    sort(a);
    printArray(a);
    

    then the output would be:

    [3, 2]
    [2, 3]
    

    As a result there is no need for sort() to return a result at all.

    You can however modify the sort() method to make a copy of the array, sort the copy and return that copy.

提交回复
热议问题