<p>I have the following code:</p>

<pre><code>var address;
getAddress(0,0);
function getAddress(latlng) 
{
  if (latlng != null) 
  {
    geocoder.getLocations(latlng, 
    function(addresses) 
    {
      if(addresses.Status.code == 200) 
      { 
        address = addresses.Placemark[0].address.toString();
        alert(address);  // Outputs something :)
      }
    });
   }
   return address;   //returns nothing :(
}
</code></pre>

<p><code>address</code> always returns <code>undefined</code> but the alert does output something. Why is this?</p>

<p>(Geocoder is an instance of <a href="http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder" rel="nofollow noreferrer">Google Maps APIs</a>)</p>

I think that your problems are all related to scope. Generally speaking, you don't want to rely on a global declaration of a variable to be used within the scope of a function.
This should correct any scope issues with your function:
```
```
var address = getAddress(0,0);

function getAddress(latlng) {
    if (latlng != null) {
        var address = geocoder.getLocations(latlng, function(addresses) {
            if(addresses.Status.code == 200) { 
                return addresses.Placemark[0].address.toString();
            }
        });
    }
return address;
}
```
```