Variables not updated when returning from a function call