0

I am learning recursive calls implementation. i get stuck with return statements, i am trying to print an array elements by recursive call. though i print but it but i want to return it, can some body help

#include<iostream>
using namespace std;

int recursive_arr_traversal(int *arr, int length_of_array) {

    if (length_of_array <= 0) {
        return 0;
    }
    else {
        return *arr; //this statement prints only one array element<endl
        cout << *arr << endl;//this works fine 
    }

    return recursive_arr_traversal(arr + 1, length_of_array - 1);

}


int main() {
    int arr[10] = { 1,2,3,4,56,7,8,9,99,0 };
    int length_of_array = 10;
    //recursive_arr_traversal(arr,length_of_array);
    cout << recursive_arr_traversal(arr, length_of_array);
    return 0;
}
11
  • What do you mean you want to return it? You shouldn't need to return anything and just print from the function itself. Commented Oct 30, 2018 at 14:35
  • Just a quick question to make you think: you have an if with a return, and an else with a return and then lastly, you call the recursive function outside of it all. What conditions have to be true for you to get to that recursive call? Commented Oct 30, 2018 at 14:36
  • @scohe001 non of if condition, that is recursion na, and that if is value giving(base condition). i could be wrong how i thought, correct me if i am thinking wrong, thankyou Commented Oct 30, 2018 at 14:39
  • @NathanOliver i want to return it so that i have a value from function which i can make use as i wish. Commented Oct 30, 2018 at 14:40
  • @bikashamit Ok, maybe I misunderstood your question Commented Oct 30, 2018 at 14:40

3 Answers 3

3

As Nathan stated, you do not need to return anything. Have a look at the following code which is your code only slightly modified, as I think you got the main idea of recursion:

#include<iostream>
using namespace std;

void recursive_arr_traversal(int *arr, int length_of_array) {

    if (length_of_array <= 0) return;

    cout << *arr << endl;//this works fine 
    recursive_arr_traversal(arr + 1, length_of_array - 1);
}


int main() {
    int arr[10] = { 1,2,3,4,56,7,8,9,99,0 };
    const int length_of_array = 10;
    //recursive_arr_traversal(arr,length_of_array);
    recursive_arr_traversal(arr, length_of_array);
    return 0;
}

In each call one element is printed and it returns once you have traversed the array. Note that there is an implicit return at the end of recursive_arr_traversal.

Sign up to request clarification or add additional context in comments.

Comments

2

A (basic) recursive function should have two parts--the base case, where all the work is already done or almost done and all we have to do is clean up and return, and the recursive case, where we need to do some small part of the work and then pass on the rest to the recusive function.

The base case here is pretty simple, and you already have it right. if(length <= 0), all we have left is an empty array, so we can just return and be done. There's no more work to do.

The recursive case is a little more difficult though and you almost have it!

Our recursive case in this problem should print the first element and then pass on an array that's one shorter and starts one element later. Also note that you never use the return value, and since this is a print function, it should probably be void. With those fixes the code could look like:

int arr_print(int *arr,int len){
   //base case: if arr is empty, we're done
   if(len <= 0) { return; }

   //recursive case: print first element, then pass array along to print rest
   cout << *arr << endl;
   arr_print(arr + 1, len - 1);
}

Comments

0

If you are saying that you want to return all the values to function like main() so that you have permanent access to your array values, that is not possible just by returning in your recursive function. As said above by others, when you return a value, that value is returned to the function that called it (draw a callstack if you need).

If you just make a global variable to save which ever values you need, that could fix your needs.

3 Comments

i wish to return it to the main recursion function and printing it every time. what wrong am i doing?
There is no way for you to return it to the first function call inside main function and print it everytime using a recursive function. If you want to do that, you need a for loop calling a printing function that returns a value. Doing a "return" does not mean that it will return to the main function, I think you are misunderstanding recursion. When you do a "return", it will return to the function call that called it.
thank you, i thought it was recursive so i tried for doing each time, thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.