title: Switch case statement in function doesn't switch C++
body: <p>I am trying to use two switch case statements in two different functions to control a micro-controller through serial packages, but one of the functions doesn't switch and the program loops indefinitely. One of the functions switches according to an <code>enum</code>:</p>
  
  <pre><code>enum class State {
  
   example_a,
   example_b,
   example_c
  
  };
  
  void serialTask(State stateArg, ObjectA exampleA, ObjectB exampleB, ObjectC exampleC, ObjectD exampleD) {
    switch (stateArg) {
      case State::example_a:
        Serial.println("Serial Task, example a");
        exampleA.doSomething();
        stateArg = State::example_a;
        break;
  
      case State::example_b:
        Serial.println("Serial Task, example b");
        exampleB.doSomething();
        stateArg = State::example_b;
        break;
  
      case State::example_c:
        Serial.println("Serial Task, example c");
        exampleC.doSomething();
        stateArg = State::example_c;
        break;
     }
  };
  </code></pre>
  
  <p>I am giving it all the other objects as arguments because depending on the case I call their functions or modify their members.</p>
  
  <p>The other function switches the value of the <code>enum</code> according to the serial port:</p>
  
  <pre><code>void stateSwitch(State stateArg) {
      if (Serial.available()) {
          uint8_t code = Serial.read();
          switch (code) {
          case 'a':
              stateArg = State::example_a;
              break;
          case 'b':
              stateArg = State::example_b;
              break;
          case 'c':
              stateArg = State::example_c;
              break;
     }
  };
  </code></pre>
  
  <p>They later get called in my <code>main.cpp</code> like this:</p>
  
  <pre><code>ObjectA aObj;
  ObjectB bObj;
  ObjectC cObj;
  ObjectD dObj;
  volatile State stateObj;
  
  void setup(){
  
  stateObj = State::example_a;
  Serial.begin(115200);
  
  
  };
  
  void loop(){
  
  stateSwitch(stateObj);
  serialTask(stateObj, aObj, bObj, cObj, dObj);
  
  };
  </code></pre>
  
  <p>This is a simplified version of the problem but it illustrates the point. The program compiles but for some reason it cannot jump out of <code>State::example_a</code>. I am using a Teensy micro-controller with the Arduino framework in PlatformIO, an extension for Visual Studio. Thank you for your help.</p>