title: How do you solve recursion which are not void
body: <p>Suppose that a second stock broker can make only one purchase, but can
  sell it on any day afterwards, that is, the stock broker can purchase the stock on some day
  i (0 &lt;= i &lt;= n - 2), and can sell it on any day from day i + 1 to day n - 1.
  Write a function
  double get_max_profit(double prices[], int n)
  that returns the maximum profit that this second stock broker can make given an array
  prices and its size n as the input. The function should return 0 if the maximum profit is
  less than or equal to 0.
  For example:
  • If prices = {1.0, 5.1, 7.3, 9.4, 4.7, 8.0, 15.0, 6.2} and n = 8, the function should return 14.0. The stock broker should purchase the stock on day 0 and sell it
  on day 6, where the maximum profit made is prices[6] - prices[0] = 15.0 - 1.0
  = 14.0.</p>
  
  <p>I tried recursion in this but its not giving the last value which is required for this question like it gives 9.1 but in the end prints zero</p>
  
  <pre><code>double get_max_profit(double prices[], int n){
      static int day = 0;
      static double max_profit = 0.0;
      printf("the profit is %lf the day is %d\n", max_profit, day);
      if (day &gt;= n-1){
          if (max_profit &lt;=0){
              return 0;
          }
          return max_profit;
      }
      for (int i = day; i &lt; n; i++){
          if (day+1 &gt;= n){
              if (max_profit &lt;=0){
                  return 0;
              }
              return max_profit;
          }
          double profit = prices[i] - prices[day];
          if (profit &gt; max_profit){
              max_profit = profit;
          }
  
      }
      day = day +1;
      get_max_profit(prices, n);
  }
  
  int main(int argc, char *argv[]) {
      double prices[] =  {2.1, 5.3, 7.7, 9.8, 1.2, 10.3, 5.0};
      int n = 7;
      double max_lim = get_max_profit(prices, n);
      printf("maximum profit is %lf \n", max_lim);
  }
  </code></pre>