Skip to main content
replaced http://cs50.stackexchange.com/ with https://cs50.stackexchange.com/
Source Link

It does return, but you have to understand where it returns to. When a return executes, it returns control to the function that called it. In this case, since BinarySearch() recursively calls itself, it returns to the previous execution of BinarySearch() that called the current recursion. It will not go directly back to main or the code that originally called BinarySearch().

Additionally, there's a serious problem with how returns are handled. The short version is that when returning, the recursive calls are not immediately returning to the previous recursion. Instead, they continue to the next line of code. For a more detailed explanation of this, see Pset3 Binary Search problemsPset3 Binary Search problems

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

It does return, but you have to understand where it returns to. When a return executes, it returns control to the function that called it. In this case, since BinarySearch() recursively calls itself, it returns to the previous execution of BinarySearch() that called the current recursion. It will not go directly back to main or the code that originally called BinarySearch().

Additionally, there's a serious problem with how returns are handled. The short version is that when returning, the recursive calls are not immediately returning to the previous recursion. Instead, they continue to the next line of code. For a more detailed explanation of this, see Pset3 Binary Search problems

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

It does return, but you have to understand where it returns to. When a return executes, it returns control to the function that called it. In this case, since BinarySearch() recursively calls itself, it returns to the previous execution of BinarySearch() that called the current recursion. It will not go directly back to main or the code that originally called BinarySearch().

Additionally, there's a serious problem with how returns are handled. The short version is that when returning, the recursive calls are not immediately returning to the previous recursion. Instead, they continue to the next line of code. For a more detailed explanation of this, see Pset3 Binary Search problems

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56

It does return, but you have to understand where it returns to. When a return executes, it returns control to the function that called it. In this case, since BinarySearch() recursively calls itself, it returns to the previous execution of BinarySearch() that called the current recursion. It will not go directly back to main or the code that originally called BinarySearch().

Additionally, there's a serious problem with how returns are handled. The short version is that when returning, the recursive calls are not immediately returning to the previous recursion. Instead, they continue to the next line of code. For a more detailed explanation of this, see Pset3 Binary Search problems

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)