Question concerning initilization of variables in a function or class!
in
Programming Questions
•
1 year ago
Hello! Hope this question is not too stupid!!
I just wrote a very simple code to illustrate a question I have as new aspiring processing coder. When I am writing code, I always come to the point that I would like to encapsulate the variables (here: float x = 0.0;) that belong to a function or class into its code to have clean logical groups. Unfortunately I cannot do that as they have to be initialized outside the function to work right!? Is there any way to initialize a variable inside a function just once and not everytime it is triggered by draw() ?
float x = 0.0;
void setup()
{
}
void draw()
{
println(countUp( 0.5));
}
float countUp( float speed){
x = x + speed;
return x;
}
I just wrote a very simple code to illustrate a question I have as new aspiring processing coder. When I am writing code, I always come to the point that I would like to encapsulate the variables (here: float x = 0.0;) that belong to a function or class into its code to have clean logical groups. Unfortunately I cannot do that as they have to be initialized outside the function to work right!? Is there any way to initialize a variable inside a function just once and not everytime it is triggered by draw() ?
float x = 0.0;
void setup()
{
}
void draw()
{
println(countUp( 0.5));
}
float countUp( float speed){
x = x + speed;
return x;
}
1