Returning a value and retain the original values

V

velthuijsen

int CalledFunc(void)
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}
 
A

Andre Kostur

int CalledFunc(void)
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}

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

or

static int loop1; loop1 = 1; // Initialization occurs every time the
loop is entered



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>

Having said those, what may be a better solution:

int CalledFunc(int & loop1)
{
for (; loop1 <= 10; ++loop1)
{
// Blah, blah blah
// return something;
}

// There should probably be a return somethingelse here
}

This way it makes it the responsibility of the caller to initialize loop1
to the correct value, allows for the caller to resume a loop (by not
initializing loop1), and to reset the loop by setting loop1 to 1.
 
V

Victor Bazarov

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;

Declare another variable

static int storedloopcounter = 1;
for (loop1=1; loop1<=10; loop1++)

Instead of counting from 1, count from storedloopcounter.

for (loop1=storedloopcounter; loop1<=10; loop1++)

Just before returning, store the loop counter:

storedloopcounter = loop1;
// return value goes here
}

}

V
 
P

Panjandrum

int CalledFunc(void)
{
for (static int loop1=1; loop1<=10; loop1++)
{
// return value goes here
}
}

Static variables should be avoided, of course. Finding a solution
without statics is not difficult in this case.
 
V

velthuijsen

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

This is correct.
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>

1 & 2 are valid points
3 I can only see a problem if you are working with threads on this and
then you'd just put the relevant serialisation inplace.
The thing I offered has a heap of other problems as well.
Best to either use your solution or if the static is being retained
move the variable declaration out of the for loop.
 
D

Donovan Rebbechi

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
}

}

Solution one: static variable:

#include <iostream>

int foo() { static int x = 0; return x++; }

int main() {
std::cout << foo() << std::endl;
std::cout << foo() << std::endl;
std::cout << foo() << std::endl;
}

// output:
// 0
// 1
// 2

solution 2: function object (same output)

#include <iostream>
class CalledFunctionObj {
int x;
public:
CalledFunctionObj() : x(0) {}
int operator() () { return x++; }
};

int main()
{
CalledFunctionObj myfunc;
std::cout << myfunc() << std::endl;
std::cout << myfunc() << std::endl;
std::cout << myfunc() << std::endl;
}

Cheers,
 
J

Jonathan Bartlett

John said:
Panjandrum wrote:




Why is that?

Same reason globals should be avoided -- implicit state almost always
leads to future programming errors. For instance, if you wanted to use
this function in another thread or in another context, the values would
stomp all over each other.

Usually it's best just to have another parameter to the function that
you pass and return via a pointer. This way the state is passed
explicitly, and you can use the function in as many contexts as you
want, since the context/state is explicitly passed.

Jon
 
R

Roy Gourgi

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
}

}
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top