0

regarding recursion in c language

in this code at return a*power(a,b-1); i not understood the concept of power(a,b-1) whats the use case of a here because it is called outside? please explain me or dry run so i can understand!! this code is for calculation of power eg a=2 b=5 then a will multiply 5 times 2*2*2*2*2 please explain me the use case of a in the power(a,b-1) #include<stdio.h> int power(int a,int b){ if(b==0){ return 1; } return a*power(a,b-1); } int main(){ int a; int b; printf("Enter an base value:"); scanf("%d",&a); printf("Enter an power value:"); scanf("%d",&b); int ans=power(a,b); printf("%d",ans); return 0; }

11th Oct 2023, 11:19 AM
Ishan Gurjar
Ishan Gurjar - avatar
10 Answers
+ 3
Actually that was not the best way to visualize the code. A better way is power(2, 5) = 2*power(2, 4) power(2, 4) = 2*power(2, 3) power(2, 3) = 2 *power(2, 2) power(2, 2) = 2*power(2, 1) power(2, 1) = 2*power(2, 0) power(2, 0) = 1 power(2, 1) = 2*1 = 2 power(2, 2) = 2*2 = 4 power(2, 3) = 2*4 = 8 power(2, 4) = 2*8 = 16 power(2, 5) = 2*16 = 32
11th Oct 2023, 1:28 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 5
Ishan Gurjar Marking an answer as "best" is supposed to show appreciation for the person who helped you and help other users to find the answer when they read your thread. Do not mark your own answer as best when it is not a solution to the problem.
11th Oct 2023, 7:03 PM
Lisa
Lisa - avatar
+ 3
Ishan Gurjar, you do not act decently by marking yourself, your beloved as the best answer. Wong Hei Ming, I tried, I spent my time trying to explain the work of recursion to you, and you...
11th Oct 2023, 2:36 PM
Solo
Solo - avatar
+ 2
Solo, the first answer was how I wrote on paper. And then realised I read the second visualization somewhere else. I myself think recursion is hard to get to and I didn't use it. But helping someone to better understand is worth to do, so I wrote the second answer.
11th Oct 2023, 2:46 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 2
Wong Hei Ming, I absolutely agree with you – this is one of the best teaching methods and yes, the second answer better reflects the sequence of recursion execution. This is easy to see in the code: https://code.sololearn.com/cv5MDdX4D5x2/?ref=app In general, everything is much simpler in this recursive function, it constantly returns the same multiplier a: power(2,5); >>> return 2*1*2*2*2*2;
11th Oct 2023, 3:34 PM
Solo
Solo - avatar
+ 1
I don’t know about c but know Python, and the recursion concept is the same. Think of a table ------return a, b a*power(a, b-1) 2, 5 2*power(2, 4) 2, 4 2*power(2, 3) 2, 3 2*power(2, 2) 2, 2 2*power(2, 1) 2, 1 2*power(2, 0) 2, 0 Which in turn a, b = 2*power(a, b-1) 2, 5 = 2 * 16 = 32 2, 4 = 2 * 8 = 16 2, 3 = 2 * 4 = 8 2, 2 = 2 * 2 = 4 2, 1 = 2 * 1 = 2 2, 0 = 1 Final result = 32
11th Oct 2023, 12:51 PM
Wong Hei Ming
Wong Hei Ming - avatar
+ 1
Ishan Gurjar, you can address each person individually through the @ symbol, after which a list of users will pop up.
12th Oct 2023, 1:20 AM
Solo
Solo - avatar
0
ok thanks..
11th Oct 2023, 1:21 PM
Ishan Gurjar
Ishan Gurjar - avatar
0
how can i say thanks to you guys *Wong Hei Ming*, *solo,* *Lisa* there is no option for repliying.. thanks i understood this power function recursion now once agian thanks..
12th Oct 2023, 1:03 AM
Ishan Gurjar
Ishan Gurjar - avatar
0
@solo thanks
12th Oct 2023, 10:56 AM
Ishan Gurjar
Ishan Gurjar - avatar