Java recursive sudoku solver, recursive function does not return correct value
By : J. Ramsay
Date : March 29 2020, 07:55 AM
Any of those help I am working a sudoku solver and am having trouble returning / ending the solver function correctly. The show() in the moveOn() function gets called and it displays the compleated sudoku fine, however solve returns false. I am trying to have solve return true when the problem is solved and null when it is unsolveable however do not know how to accomplish this. , You have only one return statement in the solve function, and that is code :
return false;
public boolean moveOn(int row, int column) {
//if the previous location was not the last in the row move to ne next cell in said row.
//if it was the last location in the row, move to the first column of the next row
if (column + 1 != L) {return solve(row, column + 1);}
else if (row + 1 != L) {return solve(row + 1, 0);}
else {show(); return true;} // reached end of grid, solved
}
public boolean solve(int row, int column) {
//pass over any numbers that are not empty
if (getSquare(row, column) != 0) {return moveOn(row, column);}
else {
//attempt to find a valid number for the location
for (int n = 1; n <= L; n++) {
if (checkRow(row, n) && checkCol(column, n) && checkSquare(row, column, n)) {
// If a number is allowed at a specific location set that location to the number
setSquare(row, column, n);
//Begin checking for a solution based on previous numbers changed
if (moveOn(row, column)) {
return true; // solved, yay!
}
}
}
//If no number is allowed in this space backtrack to the last successful number
//changed and reset all locations that have been changed recursively
setSquare(row, column, 0);
}
//If the puzzle is unsolveable
return false;
}

What is the difference between return the function and not return in a recursive function?
By : muhib raza
Date : March 29 2020, 07:55 AM
it helps some times Any function that does not return nothing (void) must encounter a return statement before running out of things to do. This is quite simply because there is no magic "return X if I do not return something" imperative, and the person using the function might rely on the return that you promised (but failed to deliver). If you now follow the path that leads to those recursive calls in the original function, you will see that the call that initiated the recursive call in the first place now has to return something. Instead it simply ignores the result of the recursive call and runs out of things to do.

RECURSIVE Function return array as NULL while var_dump before return works
By : Mike Holmes
Date : March 29 2020, 07:55 AM
Does that help EDITED: Since now I know this is called a recursive function, I have found additional information from here: , return recursive find_tree result like this: code :
if ($row['parentid'] == 0) {
var_dump($tree); // this works
return $tree; // this doesn't
echo 'hello'; // just for troubleshoot which it doesn't echo
} else {
$parentid = $row['parentid'];
return find_tree($parentid, $tree); // < HERE
}

What does a recursive function return if it does not have a return statement?
By : Black Qin
Date : March 29 2020, 07:55 AM

Recursive function returns 0 when its done. Why doesn't 'return 0' override the previous return value of the function?
By : chaw
Date : March 29 2020, 07:55 AM
With these it helps Because the first call of the function is the last one that returns. Assume you have the list [1,2,3]. You code will end up with code :
return 1 + sum([2, 3])
return 1 + (return 2 + sum([3]))
return 1 + (return 2 + (return 3 + sum([])))
return 1 + (return 2 + (return 3 + 0))
return 1 + (return 2 + 3)
return 1 + 5
return 6

