Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Android API ask for a results parameter as opposed to a return method?

My question is throughout the whole of the Android API a lot of the library calls ask for a results[] parameter to be passed into method for example:

public boolean isInProximity(double startLat, double startLongitude, float meters) {
    float[] results = new float[3];
    Location.distanceBetween(startLat, startLongitude, endLatitude, endLongitude, results);
    return results[0] <= meters;
}

The call to android is the Location.distanceBetween() and it requires the results[] why don't they just include a return signature to the method? I am assuming that it is some kind of memory or speed optimisation. I am unsure on the exact reasoning behind it.

like image 216
Hones Avatar asked Apr 02 '13 13:04

Hones


1 Answers

You are correct, it is about optimization, that way you can call this method many times without needing to allocate memory each time.

Edit -- Here is a test so: http://codeyarns.com/2010/10/21/c-return-value-versus-output-parameter/
Better late than never I guess.

like image 59
Pozzo Apps Avatar answered Oct 07 '22 05:10

Pozzo Apps