Lens hoods are primarily designed to avoid unwanted stray light from entering the lens by extending and shading the end of the lens. In addition, since the end of the lens is extended, you also get the added benefit of some extra protection from accidental impact. Dedicated lens hoods are designed to match the specific focal length of the lens it was designed for.
The main function of the lens hood is to shade the lens from stray light, improving your contrast and image quality. But it will also protects the front barrel from the inevitable impacts against walls, door frames, and other real-life obstacles and in inclement weather, it can assist in keeping moisture or wind-blown debris off the lens.
This hood enables you to shoot even towards the light source (in bright sunlight) without excessive flare, as long as you keep the light source outside the image area.