Is this called recursion.

I am also laughing at myself but really can this thing be called recursion?
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
int function3(int num){
            if(num==0)
            return 1;
            else {
            cout<<"*" <<" ";
            return function3(num-1);
            }
            }    
    
int function2(int num2)
{
    static int num=num2;
    static int acb=0;
    if(acb==num)
    return 1;
    else{
    acb=acb+1;
    function3(acb);     
    cout<<endl;
    return function2(acb);
    }  
}  
int function(int num){
     static int abc=num;
     static int acb=num;
     if(num==0 && abc==0)
     {
//     cout<<acb;
     
     return  function2(acb);
     }
     
     if(num==0){
                cout<<endl;
                abc=abc-1;
                
                num=abc;
                function(num);
                }
     else{
     cout<<"*"<<" ";
     return function(num-1);
     }
     }


calling function from main.
Yeah, why? Seems like a really horrible way to implement something but, shrug.
Recursion is a function that calls itself. All of these functions do that.

You may get some problems with that specific code specifically I am seeing that if num==0 and abc!=0 then there will be no return value for int function(). but yes, it is recursive, even with the static variables in there.
to use recursion correctly

you have to define the terminate condition, so that it wont

cause the stack overflow.
Recursion does not require a function to call itself directly. You can also have indirect recursion, where one function calls another function which eventually calls the original function. e.g.

func1 -> func2 -> func3 -> func1 -> func2 -> func3 -> ...

But direct recursion (func1 -> func1 -> func1 -> ...) is much more common and a lot easier to get your head around.
Topic archived. No new replies allowed.