Declaring Variables in Subroutines

In an effort to keep my code as simple and readable as possible, I tend to write using subroutines. To remain consistent I try to declare variables used only within a subroutine, inside the subroutine. Everything else is obviously declared globally in setup().

How do I declare variables inside a subroutine so they execute once, instead of every time the subroutine is called?

void setup(); { float impulse_purchase; }

void loop(); { // go to the next store

// Oops, there's something else I just MUST have!

impulse_purchase = 6.73;

money_in_pocket(); }

void money_in_pocket(void); {

void setup(); { float cash = 57.12; }

cash = cash - impulse_purchase;

return: }

In this case, cash is only used locally, so should probably be defined locally. I doubt the system would allow another setup() function inside a subroutine, but if I don't, cash is gong to be redefined as 57.12 every time the subroutine is called. (I'd try it, but I have so many other problems right now, I figured I'd be off asking the group.) This wouldn't be a problem if my variable was in fact a constant.

What do I do when it's not? Do all variables that change have to be declared globally?

I am not sure what you mean, but perhaps the static modifier is what you need.

Variable scope.

groundFungus: I am not sure what you mean, but perhaps the static modifier is what you need.

Variable scope.

This reply is proving to be surprisingly helpful. THANK YOU!!