V
velthuijsen
int CalledFunc(void)
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}
int CalledFunc(void)
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}
Roy said:I would like to able to able to return a value from a called function but
also retain the loop counter in the called function the next time I call it
again.
TIA
Roy
void CallingFunc(void)
{
int lp1=0;
int x=0;
for (lp1=1; lp1<=5; lp1++)
{
x=CalledFunc();
}
}
int CalledFunc(void)
{
// I would like to be able to keep the value of loop1 where I left off the
next time that I call this function. For example let's say that loop1 has a
value of 3 when I
// return a value to the CallingFunc. When I call the CalledFunc again, I
would like loop1 to have a value of 4 (3+1) and not 1;
int loop1=0;
for (loop1=1; loop1<=10; loop1++)
// return value goes here
}
}
int CalledFunc(void)
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}
Andre said:Side Question: that static int loop1=1. Is the first clause of the for
statement functionally equivalent to:
static int loop1(1); // Initialization only occurs once
There are certain caveats to this solution:
1) The OP didn't mention a return value outside the loop.. what happens
if the loop completes?
2) This preserves the loop counter, but now there's currently no way to
reset the loop counter back to 1. So once you manage to get through the
entire loop, there's currently no way to do the loop again, _ever_.
<Platform-specific>
3) Not reentrant. Two separate invocations of CalledFunc will interfere
with each other's loop1 variable.
</Platform-specific>
Hi,
I would like to able to able to return a value from a called function but
also retain the loop counter in the called function the next time I call it
again.
TIA
Roy
void CallingFunc(void)
{
int lp1=0;
int x=0;
for (lp1=1; lp1<=5; lp1++)
{
x=CalledFunc();
}
}
int CalledFunc(void)
{
// I would like to be able to keep the value of loop1 where I left off the
next time that I call this function. For example let's say that loop1 has a
value of 3 when I
// return a value to the CallingFunc. When I call the CalledFunc again, I
would like loop1 to have a value of 4 (3+1) and not 1;
int loop1=0;
for (loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}
Panjandrum said:Static variables should be avoided, of course.
John said:Panjandrum wrote:
Why is that?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.