title: How do I return a value from a Java function?
body: <p>I am trying to get the following program to work:</p>
  
  <pre><code>public class funWithNumbers {
  public static void main (String[] args) {
      int ten = 10;
      int subend = 7;
      int result = ten - subend;
      int success = 0;
      int trials = 750;
      for (int i=0; i&lt;trials; i++) {
          double randomNumber = Math.random();
          randomNumber = randomNumber * 200;
          randomNumber++;
          int randNum = (int) randomNumber;
          int mystery = randNum;
          returnTen(mystery, subend, result, success);
      }
      int accuracy = (success / trials) * 100;
      System.out.println("Trials Run: " + trials);
      System.out.println("We can say that the number " + result + " indeed equals " + result + " with " + accuracy + "% accuracy.");
  }
  public static int returnTen(int mystery, int subend, int result, int success) {
      int original = mystery;
      mystery = mystery + mystery;
      mystery = mystery * 5;
      mystery = mystery / original;
      mystery = mystery - subend;
      if (mystery == result) {
          success++;
      }
      return success;
  }
  }
  </code></pre>
  
  <p>Everything in this program works except that last statement (<code>return success</code>). For whatever reason, I cannot return the value from the <code>returnTen</code> function back to the main program. I tried debugging the program using <code>System.out.println()</code> statements along the way (not included above) and within the <code>returnTen</code> function itself, the <code>success</code> variable is successfully incremented. But I cannot get that to pass back to the main function.</p>
  
  <p>What should happen: You'll notice the function is basically a common number trick. The end result of <code>mystery</code> should be 3 (which is what result is). Therefore, <code>success</code> should always be incremented every time the function runs. Which means that accuracy should be 100%, and the statement printed out should read <code>We can say that the number 3 indeed equals 3 with 100% accuracy</code></p>
  
  <p>What is happening: The end statement currently prints <code>We can say that the number 3 indeed equals 3 with 0% accuracy</code> which is obviously not true - <code>success</code> is not being passed back and therefore, accuracy is being calculated as 0%.</p>
  
  <p><strong>EDIT:</strong> Changing <code>returnTen(mystery, subend, result, success);</code> to <code>success = returnTen(mystery, subend, result, success);</code> changes the end statement from 0% to 25000%.
  <strong>UPDATE:</strong> Fixed the problem - <code>success =</code> needed to be added and I had to change <code>success / result</code> to <code>success / trials</code> - oops!</p>