Recursion-Convert String to Integer

For this Question , I Tried Submitting in Hackerblocks as Well as In Challenge Section, but Some Compile Time Error is Showing.
Although I’ve Tried Submitting the Same in Codechef IDE, Tutorialspoint Online IDE, My own Codeblocks IDE, Everywhere Code is Running.
So, What to do?

I’m Sharing a Link, if You Try the Code in any Other IDE, It’s Running, for Example Codechef

your code has issue in String_to_Integer function.
as you defined its return type as int, it must return something. you returned number only when size is 0 whereas it should return in every possible case.

solution: return String_to_Integer(Arr+1,Size-1);// change last line of String_to_Integer function.

updated code:https://ide.codingblocks.com/s/214963
there is small change in printing output, also check that.

please rate and resolve if satisfied.
thanks

But return statement is executed only when base case encounters.
and Codechef IDE , codeblocks software is also not giving any error.
Syntax Wise I understand return should be int but logically I don’t return from this function and we return from a function once only for a given function call, which is in base case.

for this question I mean, I return when base case is encountered

as you said “we return from a function once only for a given function call”.
that is true. when we write recursive code, there are many function calls involved.
for eg. in your case if size = 5, the function will be called 6 times, but you are returning the value only once when size =0. what about returning when size is 1,2,3,4,5…
there should be 6 returns as function is called 6 times. right?

I checked other IDE, some IDEs may be automatically correcting these minute mistakes. But this kind of practice is never recommended.

lets try to understand this through another example.
fibonacci series: 0 1 1 2 3 5…so on… mathematically f(x) = f(x-1)+f(x-2)
how can we implement it recursively?

#include <bits/stdc++.h>
using namespace std;
int fib(int n){
if(n==0 || n==1)
return n;
else{
fib(n-1)+fib(n-2);
}
}
int main(){
cout<<"Number = "<<fib(4)<<endl;
return 0;
}

according to your approach , I am only returning in base condition. try to run this code in any IDE, it will never work…
you have to write -> return fib(n-1)+fib(n-2); to make this work.

thanks

Okay, Thanks, I got it

I was editing …please read it again.

please rate and resolve if satisfied. thannks

You can paste my code without return and it runs

yeah I checked it and explained in my previous comment. please check

I Understood What you Said, but don’t know why Codechef online IDE and some others are also accepting. Anyway, thank you @Gaurav :slight_smile: You Can close my doubt, I’m satisfied :slight_smile:

In the fibonacci code, I need return values of fib(n-1) and fib(n-2), but in my code particularly , I’m updating num in else part, num being static exists in all recursive calls and i’m just updating paramers in else case recursive calls, that’s why without return also it’s working.
I understood , thank you :slight_smile: