0

I'm very new to programming and would like to know if this code can also be considered a recursion since it calls itself. Also would like to know if this is good practice.

void editArr(int arr[], int arrSize){
int index;

cout << "Enter INDEX: ";
cin >> index;

if(index >= arrSize){
  cout << "INDEX is OUT OF BOUNDS" << endl;
  editArr(arr, arrSize);
}

cout << "Enter VALUE: ";
cin >> arr[index];

cout << "\n[1] Continue || [0] Exit";
cin >> choiceExit;
if(choiceExit == 1)
  editArr(arr, arrSize);

}
Chansters
  • 141
  • 1
  • 3
  • 13
  • 6
    A function calling itself is the very definition of recursion. – Some programmer dude Dec 08 '16 at 10:13
  • If it's "good practice" or not depends. In this case it seems the function is using recursion instead of a loop for the "continue/exit" choice. That specific instance of recursion I would say is *bad* practice. – Some programmer dude Dec 08 '16 at 10:15
  • Wikiquote: Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to [iteration](https://en.wikipedia.org/wiki/Iteration)) ([src](https://en.wikipedia.org/wiki/Recursion_%28computer_science%29)) – Ivan Aksamentov - Drop Dec 08 '16 at 10:15
  • @Someprogrammerdude I'd say it's definitiely not _good practice_, recursive calls are always prone to overflow the call stack. – πάντα ῥεῖ Dec 08 '16 at 10:18
  • Also, you don't need to have *direct* recursion (a function calling itself directly), you can also have *indirect* recursion. For example function `a` calls function `b` which in turn call function `a`. This is still recursion. – Some programmer dude Dec 08 '16 at 10:25
  • Just about nothing in this snippet is good practice.It contains several grave errors and recursion is one of them, though probably not the worst one. – n. m. could be an AI Dec 08 '16 at 10:33
  • When index is out of bounds you recurse, but when you exit it will go back to "Enter VALUE:" for the `index` that were out of bounds. Perhaps the rest of the code should have been in a `else` block? – Sylwester Dec 08 '16 at 11:01

3 Answers3

1

I'm very new to programming and would like to know if this code can also be considered a recursion since it calls itself.

Yes, that's how recursion is defined. A function calls itself directly or indirectly.

Also would like to know if this is good practice.

In general no. It's better to replace that code with a loop and using a stack (e.g. std::stack). In your case even a simple loop would work fine.

Recursive calls depend on the (limited) intrinsic call stack size, and are prone to overflow the call stack.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

The function calls itself, it is recursive.

The program will allocate a new stack frame every time the user choses to continue, as this could happen indefinitely, I would say a loop is a better choice here.

As a side note, this function is tail recursive (except when an OOB index is given, then all hell breaks loose), so the compiler may well optimize out allocating the extra stack frame, personally I would not rely on that though.

Community
  • 1
  • 1
jayjay
  • 1,017
  • 1
  • 11
  • 23
0

It seems it will work. Don't forget to define choiceExit as an integer at the top of the function. Note that it's not the usual way to initialize an array,recursion is a bit "heavy". Also it's important to note that you don't need to put Arraysize as a parameter as size(Arr) will give it. A while loop would be indeed much better. ChoiceExit could be a boolean by the way, as there are only two "cases" possible