doubt related to recursive functions

Is it possible to break/exit out of a recursive function?
I'm trying to write a code where I'm generating the binary representation of a number for several different numbers and i want to count the total number of terms for each number; so when I return the value I end up with the initial value of "count"(my counting variable). Is it possible to just return the last vaklue of "count"?
Why not put the code inside a loop instead of a function?
Of course, otherwise it would simply keep looping forever and would not be usefull in any way, here is an example of a successful recursive function that calculates the factorial of a number.
1
2
3
4
5
6
7
unsigned long factorial( unsigned long number )
{
     if ( number <= 1 )
             return 1;// base cases 1 and 0 
     else
             return number * factorial( number - 1 );
}


when function factorial calls itself a new copy of the variables are created so using a static local variable would not work.

could you post your code?
hey thanks..i just realised i was calling the function instead of returning the function call

my code is something like this:(im doing a double base representation now)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#define SIZE_OF_INTEGER 100
#define BASE_1 2
#define BASE_2 3
#define NUM 2000

ZZ bs(vec_ZZ& a, long  beg, long end, ZZ x)
{
        long mid;
        mid=(beg+end)/2;
        if (x==a[mid])
        {
                cout << a[mid];
                return a[mid];
        }
        else if (beg>end)
        {
                cout<<a[end]<<" + ";
                return a[end];
        }
        else if (x<a[mid])
        {
                end=mid-1;
                return bs(a,beg,end,x);
        }
        else if (x>a[mid])
        {
                beg=mid+1;
                return bs(a,beg,end,x);
        }
}

ZZ alg(vec_ZZ& a, long s, ZZ  num, ZZ count)
{
        ZZ num1;
        num1=num-bs(a,0,s,num);
        if (num1==0)
                return count;
        else
                return alg(a,s,num1,++count);

}
main()
{
        ZZ d;
        vec_ZZ a,b,c;
        a.SetLength(power_long(SIZE_OF_INTEGER,2));
        b.SetLength(NUM);
        c.SetLength(NUM);
        ZZ v;
        v=power_ZZ(2,SIZE_OF_INTEGER);
        long x=0;
        for(long i=0;i<=SIZE_OF_INTEGER;i++)
        {
                for(long j=0;( d = power_ZZ(BASE_1,i)*power_ZZ(BASE_2,j)) <= v ;j++)
                {
                                a[x]= d;
                                x++;
                }
        }
        sort(a,x);
        ZZ cnt;
        cnt=0;
        for(long k=0;k<NUM;k++)
        {
                c[k]=1;
                b[k]=rand();
                cout<<endl<<b[k]<<" : ";
                c[k]=alg(a,x-1,b[k],c[k]);
                cnt=cnt+c[k];
        }
        cout<<endl<<"total no. of terms: "<<cnt<<endl;
        cout<<"avg no. of terms= "<<cnt/NUM;
        cout << endl;
}


i had not used return alg in the else part..i had just called alg

and incase you're wondering, the ZZ stuff is from NTL.
thanks!!
:)
Last edited on
Topic archived. No new replies allowed.