Change variables within user defined functions?

hay guys
i think i know the answer but ill ask anyway.
Is it possible to store a changes to a variable within a user defined function (UDF), so that the UDF will perform differently each cycle, even if there is no input value?

So in the example code the counter would go up rather than resetting each time.

int printCount = 0;

void setup() {
Serial.begin(9600);
}

void loop() {
printCount = counter();
Serial.println(printCount); // prin value generated by udf

}



int counter(){    //user define funtion. simple counter
 int i = 0;       //set startining value of i
 i = ++i;     //increas value of i
  return i;
}

Use static:

 static int i = 0;       //set startining value of i

That won't compile. Redefinition of i in your function. Can't have two variables there with the same name. Go learn what the "int" does. Then learn the "static" keyword to make it keep its value from call to call.

Can you just put

int I= 0;
Prior to void setup();

So it is declared only once as a global
Variable And then not declare it within your function ?
It should then hold its value outside of the function and can be acted on outside of the function

If (I>16)..... for example

Yes but if you're going to make it global give it a better name.

the doable int was just a typo.
i think u guys r missing the point. The code is just an example. im asking if the is a way to change the value of a verible within the function and store the change for the next time the code is used

Yes , and declaring the variable you want to use globally is the way .
You can for example read an input within your function and change a variable value . As it was declared globally it won't change outside of the function or anywhere else unless you want it changed and could used within a diffeeent function for example

You can't declare new variable tho
, if that's what you mean

SavageRodent:
the doable int was just a typo.
i think u guys r missing the point. The code is just an example. im asking if the is a way to change the value of a verible within the function and store the change for the next time the code is used

No, we're not. Static will do exactly what you want. It lets you keep a value private to a function that retains its value between calls.

No, you're missing the point. You've been given two different ways to do it.

thanks wildbill static was exactly what i was after.
sorry i missed it, got detracted by Delta_G nitpicking.

Nitpicking? What are you talking about? I pointed out an error in the code you posted. You'd be surprised how many people don't know that. How was I supposed to know whether you did?

Maybe if you had actually engaged your brain and read my response instead of getting your panties all bunched up because someone pointed out your mistake you would have seen the answer you wanted was there too.

hammy:
Yes , and declaring the variable you want to use globally is the way .

Well, it is a way. As has already been pointed out, 'static' is a better way.

WARNING:

 i = ++i;     //increas value of i

This statement has "implementation defined" behavior. In other words, it can do different things depending on the compiler. I think in Arduino versions up to 1.0.6 it would increment 'i' but in 1.6.0 and later it would leave 'i' unchanged.

Use one of these statements instead:

i++;
++i;
i += 1;
i = i + 1;

Watch out, this one would rather have his code not work than to have someone "nitpick" and teach him how to do better.