i have seen in many recursive calls, sometimes they use return recursivefuunction() or sometimes they just use the recursivefunction()(without that return keyword).
i want to know what is the difference between these two recursive calls.
Basic recursion doubt
Hello @sktg99,
It is due to the return type of the recursive function.
- Suppose if you want to return some value from the base case back to the recursive_function call(main function).
- else, you don’t use return keyword before recursivefuunction().
Example:
Check if an array is sorted(increasing order) or not?
1.
bool array_sort(int arr[],int i,int n){
//Base case 1:
if(i==n){
return true;
}
//Base case 2:
if(arr[i]>arr[i+1]){
return false;
}
//Recursion step:
return array_sort(arr,i+1,n);
}
int main(){
//function call:
cout<<array_sort(arr,0,n-1);
}
void array_sort(int arr[],int i,int n){
//Base case 1:
if(i==n){
cout<<true;
return;
}
//Base case 2:
if(arr[i]>arr[i+1]){
cout<<false;
return;
}
//Recursion step:
array_sort(arr,i+1,n);
}
int main(){
//function call:
array_sort(arr,0,n-1)
}
Hope, this would help.
Give a like if you are satisfied.