title: Javascript function returns undefined when called from another function
body: <p>When I call a function within a function it returns <code>undefined</code>. When the function is called by itself it returns the data I expect it to return.</p>
  
  <p>The functions:</p>
  
  <pre><code>function companyNumbers(account_id) {
      $.ajax({
          type: "GET",
          url: "http://example.com/api/api_client_relationships/" + account_id,
          dataType: 'json',
          success: function(data) {
              for(var i=0;i&lt;data.length;i++){
                  console.log(companyNames(data[i].company_id)); // returns undefined
              }
          }
      });
  }
  
  function companyNames(id) {
      $.ajax({
          type: "GET",
          url: "http://example.com/api/api_company_names/" + id,
          dataType: 'text',
          success: function(data) {
              return data; // returns valid result when not called within another function
          }
      });
  }
  </code></pre>
  
  <p><code>data[i].company_id</code> is just a value parsed from the returned json response. It acts as the argument passed to the companyNames function.</p>