Davood:
I do not have time to rewrite and test your example code, but here is a simpler example:
a = 1;
b = 2;
c = 30;
While[
a < c,
a = a * b;
]
{a, c}
Can be turned into the following function:
f[a_Integer, b_Integer, c_Integer] := Module[
{
retVal
},
retVal = a;
While[
retVal < c,
retVal = retVal * b;
];
{retVal, c}
];
And then called as follows:
f[1, 2, 30]
Which returns:
{32, 30}
Note that I had to declare a privately scoped variable (retVal) for Module because parameters to functions are immutable. You do not always have to do this, but it is helpful to know how.