State Variable vs Argument

In this lesson, Ben sets up the ShowLevelReward() function to use the state variable called “level” to determine which reward to show. Makes sense, of course. Rather than using a state variable, another option could have been to pass the level variable as an argument to ShowLevelReward().

I see one reason why he would have chosen the route he did… why pass an argument to the function if that variable is already available? Seems like extra data to move around and manage for no good reason. But are there reasons beyond that? Is there a performance benefit, generally, to using global state variables instead of arguments? Is it seen as coding best practices? Is it personal preference?

Thanks! Loving these courses so far.

Privacy & Terms