1. ## Cyclic function

Hello !
I am a beginner in C programming and I came across a problem while taking a test. The requirement sounded something like this: Write a function that is called cyclically and displays the current value as well as the last two values of a variable. Does anybody have any tips on how to do this because I have absolutely no idea. Please keep in mind that I am new to this.
Thank you !

2. Step 1: Be sure the problem statement is completely understood.
Step 2: Determine the solution to the problem, in general terms.
Step 3: Refine the problem solution into discrete steps. A pen and paper will come in handy for this part.
Step 4: Begin implementing the solution in code. Build the code gradually, testing as you go. A development process

It sounds like your "cyclically" function should be able to take a value, since local variables need to be updated. You may also want to read up on static variables.

3. Trust me, I tried to wrap my mind around it. But I have no idea where to start at. For me, the question is very vague but this is the way they formulated it.

4. Perhaps start by writing a program that calls a "cyclically()" function, which simply receives and prints a value. Call this function a few times in a loop.

If you can get that far, we can proceed from there.

5. So..maybe something like this?

Code:
```#include <stdio.h>
#include <stdlib.h>
int cyclical(int);
int main()
{
int x;
while(x!=0)

printf("x = %d \n", cyclical(x));

}

int cyclical(int n)
{

printf("Input value: ");
scanf("%d", &n);

return n;
}```

6. Not quite; I was thinking more along the lines of simply printing its argument:

Code:
```void cyclical(int n)
{
printf("%d\n", n);
}```
And instead of a while, use a for loop so you can just print it a certain number of times (maybe 5 times). No input is needed at this stage.

Code:
```#include <stdio.h>
#include <stdlib.h>
void cyclical(int n);
int main()
{
int i;
for(i=0; i<5; i++)
cyclical(i);

}

void cyclical(int n)

{
printf("%d\n", n);

}```

8. Perfect! Now you can continue to develop the function.

You will need a way for your function to "remember" previous values. Have you looked into static variables yet?

9. Kind of. I know that static variables keep their values between two function calls.

10. Correct (though I would say between successive function calls).

According to the requirements you posted, the function needs to print the current value of a variable, as well as its previous two values.

Think about how static variables might help achieve this.

11. I honestly have no idea. Could you give me a small hint ?

12. Sorry to step in when Matticus (the master!) is at work, but I would suggest firstly only saving the previous value instead of the two previous values.
You need to define a static int variable in your function to hold the previous value.

13. I gave you pretty much all the pieces, and algorism gave you the hint you requested. Let's see what you come up with.

On a side note, there is a certain etiquette when posting to help forums, which includes not posting to multiple forums at once. If you want to get the most out of such communities, I suggest reading this (do not be taken aback by the title, it's actually a very informative read).