I have a function which is called several times, and I want the variables declared in the function to retain their values when I call the function again, without having to pass them in all the time.
Is that possible? For example, in the following code, I don't want bleh's value to be reset when I call my_func again. After the 1st iteration bleh's value is 7, and I want it to begin as 7 in the 2nd iteration.
Is there a way to do that?
Is that possible? For example, in the following code, I don't want bleh's value to be reset when I call my_func again. After the 1st iteration bleh's value is 7, and I want it to begin as 7 in the 2nd iteration.
Is there a way to do that?
Code:
#include <stdio.h>
#include <stdlib.h>
int my_func(int x, int time);
int main()
{
int i;
int j;
j= 7;
for (i=0; i<10; i++)
{
my_func(j, i);
j=j+i;
}
return 0;
}
int my_func(int x, int time)
{
int bleh;
if (time == 0)
bleh = 0;
printf("Before bleh = %d\n", bleh);
bleh = bleh + x;
printf("After bleh = %d\n", bleh);
return 0;
}