4 ms·
This is why it's usually a good idea to minimize mutable state, especially global mutable state. Pure functional languages like Haskell go even further and pro
by tgflynn 4y ago
This is why it's usually a good idea to minimize mutable state, especially global mutable state. Pure functional languages like Haskell go even further and prohibit mutable state entirely.
One way to rewrite your example to do what you want would be with the following changes:
int g_a = 0;
int g_b = 0;
void update_state( int a, int b ) {
g_a = b + 1;
g_b = a + 1;
}
Then call update_state like this:
update_state( g_a, g_b );
This works because in C function parameters are passed by value by default.
As for languages that distinguish "old" from "new" values, I don't know of any and I'm not sure there's a well-defined way to do it apart from function calls.
- ironmagma 4y agoIt's a slight technicality, but Haskell does allow mutable state, it just has to be wrapped in a monad (e.g. IO or MutVar#).