title: How would one rewrite a program to eliminate side effects?
body: <p>I'm trying to research this problem in a java textbook.
  The problem is to rewrite this program to eliminate the side effects,
  where a change to the state of an object or program variable is produced by a call on a function (or a method, which is kinda the same thing).</p>
  
  <p>package sideeffect;</p>
  
  <pre><code>public class SideEffect {
  
      **public static int x;**
  
      public static int f(int n){
          x = x * 2;
          return x + n;
      }
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          x = 5;
  
          //int result = f(x) + f(x);
          int result = 2 * f(x);
          //System.out.println(result);
      }
  
  }
  </code></pre>
  
  <p>The commented out lines are what happens when I implement the integer variable and it's printout.
  If I implement those lines and comment out the other lines,
  the result is 45.
  If implement the other one,
  the result is 30.
  Is there a way to straighten out the program so there are no side effects?
  I was thinking I remove the global variable line since it says in the book that it can lead to this bad,
  confusing code.
  But what else can I try?</p>