You are expressly stating that the function f returns an int, and the problem is there are scenarios in this function whereby the condition is not meet any of your if...elseif conditions:
package main
import "fmt"
func main() {
fmt.Println(Paths(0, 0))
fmt.Println(Paths(1, 0))
fmt.Println(Paths(0, 1))
}
func Paths(m int, n int) int {
length := m
width := n
var f func(int, int) int
f = func(h int, v int) int {
if h == length && v == width {
return 1
} else if h < width && v < width {
return f(h, v+1) + f(h+1, v)
} else if v < length {
return f(h, v+1)
} else if h < width {
return f(h+1, v)
} else { // this condition does occur
return 0
}
} // Line 19
return f(1, 1)
}
So these scenarios are allowed by the function. You might validate these values before entering this function, but the problem is this function does not do any of that, and it is valid to pass those values through.
If you are confident that 0,0; 0,1; and 1,0 will not be the input values, you can avoid the last else if h < width {
by just having an else; however this will change the logic of the function. Given, also, that the function Paths is exported, you should probably do validation on the values coming in, if you need to ensure that the 0,0; 0,1; and 1;0 options are not permissible.
If it suits your purpose, you could just have a return right at the end of the function, for returning 0 or -1 to indicate that the value is incorrect. Personally, if these values are not acceptable in the function, it would be best to validate the input and return an error or if you must, panic.
func Paths(m int, n int) int {
length := m
width := n
var f func(int, int) int
f = func(h int, v int) int {
if h == length && v == width {
return 1
} else if h < width && v < width {
return f(h, v+1) + f(h+1, v)
} else if v < length {
return f(h, v+1)
} else if h < width {
return f(h+1, v)
}
return 0
}
return f(1, 1)
}