It works when the code is in loop() because the changes to state affect the global variable named state, not the function argument named state. I told you that changes made in the function are lost, but you seem to have ignored that.
Google pass-by-reference and pass-by-value to understand passing data to functions.