# Function variables are faster than functions?

I was doing a leetcode problem, and discovered that this code:

``````func floodFill(image [][]int, sr int, sc int, color int) [][]int {
original :=image[sr][sc]
if original == color {
return image
}

fill(image,  sr, sc, color, original)

return image
}

func fill(image [][]int, sr int, sc int, color int, original int) {
image[sr][sc] = color

if sr > 0 && image[sr-1][sc] == original {
fill(image,  sr-1, sc, color, original)
}

if sc > 0 && image[sr][sc-1] == original {
fill(image,   sr, sc-1, color, original)
}

if sc < len(image[0]) - 1 && image[sr][sc+1] == original {
fill(image,  sr, sc+1, color, original)
}

if sr < len(image) - 1 && image[sr+1][sc] == original {
fill(image,  sr+1, sc, color, original)
}
}
``````

is actually slower (10ms) than this other code (3ms)

``````func floodFill(image [][]int, sr int, sc int, color int) [][]int {
original :=image[sr][sc]
if original == color {
return image
}

var fill func( sr int, sc int )
fill = func(  sr int, sc int ) {
image[sr][sc] = color

if sr > 0 && image[sr-1][sc] == original {
fill(  sr-1, sc )
}

if sc > 0 && image[sr][sc-1] == original {
fill(   sr, sc-1 )
}

if sc < len(image[0]) - 1 && image[sr][sc+1] == original {
fill(  sr, sc+1 )
}

if sr < len(image) - 1 && image[sr+1][sc] == original {
fill(  sr+1, sc )
}
}

fill(  sr, sc )

return image
}

``````

Is this a side effect of their platform (the instance that ran the first version is a bit faster), or is this a clue that func variables are faster than functions ?

Hi! Closure vs function has no difference. What makes your code faster is the less closure arguments number; in fact you’re using an in loco instance of `image[][]` instead of passing it call by call (the first case + `color` + `original`).
It’s one of the advantages of lambdas in call-by-value programming languages (if well used).
Ah and …don’t take leetcode times as the holy grail, if you run your program several times indeed you always have different (sometimes a lot) results.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.