# Thread: Can someone explain to me what this function is doing?

1. ## Can someone explain to me what this function is doing?

Hi guys I'm confused on what exactly this function is doing.

Code:
```int strangefunction(int n) {
int c;
for (int c = 1; c != 0; ) {
if (n > 100) {
n = n - 10; c--; }
else {
n = n + 11; c++; }
}
return n;
}```
Any ideas?

2. What's stopping you from tracing through it? Pick an n (say, 22, or 45, or 78) and "be the computer".

3. well I see what is returning when I enter in numbers, I just don't see a pattern, it just seems like just

if (n > 100)
subtract 10

else

4. Yes, but when does the loop end?

--
Mats

5. Well it also increments or decrements the c variable, which will impact how the loop is executed.

It may have helped to put braces on separate lines and keep it to one statement per line so that you don't miss a step.

As far as n goes, a very short loop is n = 100:
Code:
```n   c
111 2
101 1
91  0```
And n is 91.

6. This looks like a homework question. I am surprised you got so many answers given that fact. It must have been your lucky day.

7. thanks guys!

I was just making sure it didn't have any mathematical relevance, which I now see it doesn't.

Well knowing what the function does isn't the homework, rewriting this function in a functional language (scheme) is the homework, so no, this isn't a homework question, I was just curious if it had any under lying relevance.

8. Ah ok, well in that case, we all love logical problems (particularly ones that require disproving a solution--and especially disproving one anothers' solutions).