# Help create a function.

• Feb 28, 2013, 04:50
fz21z
Help create a function.
My question is how to make a function of this condition:

Code:

```for(i=0;i<30;i++) {   if(i==0)   {     if(a[i].c<50) a[i].y--;     else if(a[i].c>=50 && a[i].c<100) a[i].y++;     else {a[i].c=0; a[i].y=65;}     a[i]++;   }   //[...]   else if(i==3)   {     if(a[i].c<50) a[i].y--;     else if(a[i].c>=50 && a[i].c<100) a[i].y++;     else {a[i].c=0; a[i].y=65;}     a[i]++;   }   //[...] }```
I would like to shorten the code:
Code:

```if(i==0) f(a[i].c,a[i].y); //[...] else if(i==3)  f(a[i].c,a[i].y);```
I'm trying this:
Code:

```function f(i,Y) { if(i<50) Y--; else if(i>=50 && i<100) Y++; else {i=0; Y=65;} i++; }```
But it ds'not work.
How to do it correctly?
• Feb 28, 2013, 11:20
felgall
You don't need a function to shorten that code as a switch statement cal shorten it without needing a function call:

Code:

```for(i=0;i<30;i++) {   switch(i) {   case 1: case 3:     if(a[i].c<50) a[i].y--;     else if(a[i].c>=50 && a[i].c<100) a[i].y++;     else {a[i].c=0; a[i].y=65;}     a[i]++;     break;   //[...]   default:   //[...]   } }```