How to make few copies of one function

I have made function with static elements inside and when I have 1 initialization of this function in the program evrything works fine.

But when I make copies of this function, it seems that inside static variable is common for all this functions.

I solved this problem by use of *ptr on input as outside memory of the function.
But does it's possible to write something:

myfunction f1(), f2(), f3()?
What you want is a class.

1
2
3
4
5
6
int foo(){
    static int n = 0;
    auto ret = n * n + n;
    n++;
    return ret;
}
becomes
1
2
3
4
5
6
7
8
9
class foo{
    int n = 0;
public:
    int operator()(){
        auto ret = n * n + n;
        n++;
        return ret;
    }
};
Thanks that give me right direction to go!!!!
I tried but class works the same:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class PWM {
	protected:
		int32_t ch_dir; // =1 - increase pwm, -1 - decreese pwm
	private:
		uint32_t CH_DC; // max Auto Reload Register (ARR)
	public:
		PWM() // конструктор за умовчуванням
		{
			ch_dir=1;
			CH_DC=0;
		}
		void SoftBlink(TIM_HandleTypeDef *htim, uint32_t Channel, int step, uint32_t max_ARR){
			   while ((CH_DC < max_ARR) & (ch_dir >=0)) {
			    	switch (Channel){
			    		case TIM_CHANNEL_1 : {
			    			htim->Instance->CCR1 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_2 : {
			    			htim->Instance->CCR2 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_3 : {
			    			htim->Instance->CCR3 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_4 : {
			    			htim->Instance->CCR4 = CH_DC;
			    			break;
			    		}
			    		default :
			    			assert(0);
			    	} // end_switch
			    		CH_DC += step;
				    	HAL_Delay(1);
			    } // end_while

			    while ((CH_DC > 0) & (ch_dir <0)) {
			    	switch (Channel){
			    		case TIM_CHANNEL_1 : {
			    			htim->Instance->CCR1 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_2 : {
			    			htim->Instance->CCR2 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_3 : {
			    			htim->Instance->CCR3 = CH_DC;
			    			break;
			    		}
			    		case TIM_CHANNEL_4 : {
			    			htim->Instance->CCR4 = CH_DC;
			    			break;
			    		}
			    	} // end_switch
			    	CH_DC -=step;
			    	HAL_Delay(1);
			    } // end_while


			  if (ch_dir>=0) {
				  ch_dir =-1;
			  }
			  else {
				  ch_dir=1;
			  }
		  } //end SoftBlink
}; // end class 

1
2
3
4
5
6
7
8
9
10
main(){
while(1){
    	 PWM p1,p2,p3,p4;
	 p1.SoftBlink(&htim2, TIM_CHANNEL_1, 1, max_ARR);
	 p2.SoftBlink(&htim2, TIM_CHANNEL_2, 1, max_ARR);
	 p3.SoftBlink(&htim2, TIM_CHANNEL_3, 1, max_ARR);
	 p4.SoftBlink(&htim2, TIM_CHANNEL_4, 1, max_ARR);
}
return 
}

I can get to work this program in hundred of ways by rewritng this code.
But I wonder why it doesn't save to memory inside variables.
Compiler forbide to create static variable inside class.
I don't know what you're trying to do, so I really can't make any suggestions. You'll have to give more information.
Ok, I have solved this problem.
I made a mistake - I define class inside of while(), but I need to create it before while, then everything works fine.
Thank you so much, Helios, classes are solved this problem.
Topic archived. No new replies allowed.