Function returns values in continue

Hi,

I am writing a program with a function that includes a long loop. I need this function to return a value when each loop is done, to send this value to output, in order to follow the progression. But I don't know how to do it in easy way.

The function is like follow:
1
2
3
4
5
6
7
8
9
int goC()
{
	... // some local value definition
	for(int i = 0; i < 1000; i++)
	{
		... // a lot of calculations done here
		return i; // -> return the value after each loop is done
	}
}

Here it only returns one value, i = 0. Clearly it's wrong.
Could anyone please help me with it? Thanks a lot!
Last edited on
Use a callback.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <functional>

int foo( int n, std::function< void(int) > notify )
{
	int sum = 0 ;
	for( int i = 1 ; i < n ; ++i )
	{
		sum += i*i ;
		notify(sum) ;
	}
	return sum ;
}

int main()
{
    const auto report_progress = [] ( int s )
            { std::cout << "intermediate value returned: " << s << '\n' ; };
    foo( 10, report_progress ) ;
}

http://ideone.com/DAaQpP
instead of having the loop occur in the function, the loop needs to be outside of the function, so that the function only gets called once every iteration of the loop.

or...

return a pointer to an int array.
Thanks for the replies.
@JLBorges: could you please explain the report_progress? is it a lambda expression ?

@pogrady: too complicated to put the loop outside... about returning a pointer to int array, do you mean to define an array of values of i to control the loop, and return the pointer? Could you make it clearer please?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
int* foo(int Size)
{
    int* Summations = new int[Size];

   for(unsigned int x = 0; x < Size; x++)
   {
       Summations[x] = x;
       Summations[x] += x;
    }

    return Summations;
};


> could you please explain the report_progress? is it a function or a value ?

It is a lambda expression; evaluating the expression yields a callable object, which can be used like a function.
http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=254


> with an auto it makes confusing.

The auto is used for type deduction; with it we ask the compiler to figure out the type of report_progress
http://www.stroustrup.com/C++11FAQ.html#auto


The same code, using a C-style callback:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>

int foo( int n, void (*notify)(int) )
{
	int sum = 0 ;
	for( int i = 1 ; i < n ; ++i )
	{
		sum += i*i ;
		notify(sum) ;
	}
	return sum ;
}

void report_progress( int s )
{ printf( "intermediate value returned: %d\n", s ) ; }

int main()
{
    foo( 10, report_progress ) ;
}

http://ideone.com/db8vl9
@pogrady: ok. but, it seems that this function doesn't return the value of "x" at the same time as "x"'s value changes in the loop. am I right?

@JLBorges: actually I need the return value and send it to a "progress bar" of Qt, to let the progress bar show the progression. the code will look like:
1
2
 const auto report_progress = [] ( int s )
            { ui.progressBar->setValue(s) ; };

ui is a kind of local pointer of Gui, setValue is a function of Qt to give value to the bar.
But this cannot work with lamdba expression. How can I rewrite it in normal form?
> ui is a kind of local pointer of Gui

A lambda capture gives the lambda expression access to variables that are declared in the enclosing scope.
http://software.intel.com/sites/products/documentation/doclib/stdxe/2013/composerxe/compiler/cpp-lin/GUID-835983D0-9779-422E-B339-0205358CAACC.htm

Something like:
1
2
3
4
5
6
7
8
9
Gui* ui = ... ; 

// ...

const auto report_progress = [ui] ( int s ) // capture ui by value
            { ui->progressBar->setValue(s) ; };

const auto report_progress_2 = [&ui] ( int s ) // capture ui by reference
            { ui->progressBar->setValue(s) ; };
I think I don't know Qt very well, that's why I have tried all these solutions, it can be compiled, but it doesn't work on GUI.

However, when I use a normal form (means not lambda), the Gui can show the value returned. the only problem was just I could not get the number of loop to be updated at the same time as the loop progresses.

Is there is simple way to get this? just make the function return its loop value in real time? then I do ui->progressBar->setValue(s) each times while the value changes. thanks.
Then you want to use the lambda expression to update the value of the progress bar, or pass a function pointer to the function. Its essentially the same thing.
Thanks. The callback method finally works.

Just want to know, is there other way to make it still more simple (even if it's already not hard to use)? Thanks.
Topic archived. No new replies allowed.