How to return value from a while in in any function

Suppose I have the following function containing while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;
 
int Loop ()
{
   // Local variable declaration:
   int a = 10;

   // while loop execution
   while( a < 20 )
   {
       //cout << "value of a: " << a << endl;
       return a;
       a++;
   }
}
int main()
{
   int b;
   b = Loop();
   cout << "value of a is: "<< b << endl;
}


here in above code I want to return value of 'a' by calling function 'Loop'. Is there any way to do this??

for above code output should be:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Last edited on
When a function returns, it is finished and moves to the next line after the call.
you would be better off putting the cout << "value of a is: "<< a << endl;
inside the loop and removing the return a; from inside as the loop will only run once as you are leaving the function as soon as the return statement is hit.

It really depends on what your overall goal of the program is. How you want to use a in the code. If your only goal is to print the value of a then the function you have is really not needed.
Last edited on
you could declare the variable 'a' outside of the function and then pass it into the function by reference.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
 
void Loop(int &a)
{
      // recursion loop execution
   if( a < 20 )
   {
       cout << "value of a: " << a << endl;
       a++;
       Loop(a);
   }
}
int main()
{
   int a = 10;
   Loop(a);
   
 return 0;
} 


Last edited on
for above code output should be:...

Are you sure? If that is your intention then you have two glaring errors in that function.

@manga: Actually you are using
cout
to show the output, but what I want is the function
Loop()
should return value of
a
so that I can use value of 'a' in another function.
Last edited on
here in above code I want to return value of 'a' by calling function 'Loop'. Is there any way to do this??

Not as described, no.

Seems like a weird question, but is this what you're looking for?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
 
int Loop(int &a)
{
      // recursion loop execution
   if( a < 20 )
   {
       cout << "value of a: " << a << endl;
       a++;
       Loop(a);
   } else
   {
	   return a;
   }
}
int main()
{
   int a = 10;
   int Final = Loop(a);
   cout << "The function returned the value: " << Final << endl;
   system("pause");
 return 0;
} 
Seems like a weird question, but is this what you're looking for?


That particular implementation results in undefined behavior. I'm not sure why you folks supplying recursive non-solutions are using a reference.

1
2
3
4
5
6
7
8
9
int Loop(int a)
{
   std:: cout << "value of a: " << a << '\n' ;

    if ( a == 20 )
        return a ;

    return Loop(a+1) ;
}


With the rather generic looking code and description the OP gave, it is difficult to figure out what he's after. Perhaps if he could describe what he's trying to accomplish instead of describing the solution he's trying to implement, we could be of more help.
I based my code off of the desired output he displayed. I passed by reference so that 'int a' changes value.
Topic archived. No new replies allowed.