The (fitness) function is used to rate how successful a snake was. Obviously eating a lot of food will give a higher fitness score. But you can further adjust the function to for example lower the fitness score when a snake turns. This would cause a snake that took less turns to eat 10 food have a higher score than a snake that ate 10 food but took more turns to do so.
If you want to learn more about this you can search for 'fitness function' in combination with genetic algorithms. There are a lot of resources out there that explain it in more depth :)