C function arguments are pass-by-value. This means that instead of passing a reference to stage
you are passing the value stored in it. The update you do the in changeStage
function then only applies to the copy that has been made.
If you want to update a variable in another function, you will need to pass a pointer to it.
void changeStage(int* stage_p){
*stage_p = 1;
}
int main() {
//...
while(stage!=0){
//if snake hits wall
changeStage(&stage);
}
}
&stage
says to take the address of stage
and pass that to the function. The stage_p
argument will then point to the int in the main.
*stage_p
causes it to use the value pointed to by stage_p
, which is stage
in the main in your case.
Further reading