The function of a lens hood (sun shade) is no different for a rifle scope than it is for a camera. The light you want to enter the tube is the light coming from down field, within your field of view, that is reflected off what you're looking at, back in the direction of you, the shooter. This light is focussed by the optics to an image that comprises your target and what's around it.
Any additional light that comes in from farther off axis than the cone of your optic's field of view of the target area will enter the tube at such an angle that it isn't part of the image you want to see. It will bounce off the inside of the tube, and instead of being refracted through the lens system, as part of the image, it will bounce through the lenses, and off the inside of the tube, at odd angles. This light DOES add additional brightness, in a sense, but it's not a good thing. It will actually have the effect of diminishing the contrast of the image of the target (that's the difference between light and dark). This kind of distortion is called "flare". The image you see really may be observably brighter, but the subject -- the target -- will be more or less washed out, depending on how much of this stray light is bouncing around and ends up on your retina.
Additional flare, less than this off-axis ambient light which is the big offender, occurs between lens elements within the tube, when light that should become part of the target image bounces off the fronts of internal elements, and then off the inside of the tube, or the back of the element ahead of the one doing the unintended reflecting. (With any good lens system, of course, a lot more light is transmitted through each lens element than bounces off its front.)
There are three major methods by which flare can be controlled in a rifle scope or photographic lens.
The first is anti-reflective coatings on the glass lens elements, which help bounce light coming in at these stray angles back off the front element, or the fronts of internal elements, so that it won't end up in the wrong place. Additionally, these coatings are tinted in such a way, and composed of materials, that help refract various colors of light that compose the image so that they focus to the same point. (This helps control 'fringing', or halos.) Older scopes, say prior to 1965 or so, may have a single purple or amber layer of coatings on its glass elements. Sometime around then, bilayer coatings were introduced: a purple layer and an amber layer. You can see both of these, if you look at a coated element's reflection of a light source, at an angle. During the latter part of the 1970s, "multicoating" was introduced, using additional layers to further control various colors of light. You may see green, or red, or yellow reflections in addition to the purple or amber.
The second method of controlling flare in a lens system is the black coating inside the barrel of the system (the tube), and the rim of the lens mount. Ideally, this coating is a very flat matte deep dead black. If it were perfect, ALL light that strikes it would be absorbed forever; this is desirable, because any light entering the lens system at an angle that is farther out than the lens' field of view isn't part of what you want to see, anyway, and would only wash out the image. (A really fine lens system will also have matte black coatings on the tube-facing edges of its elements.) As I said before, it might be a bit brighter overall, but that is at the cost of diminished contrast, your ability resolve detail, and -- in very dim conditions, it doesn't help your night adaptation, either.
The final way of controlling this off-axis light is to constrict the angle at which light can strike the front element to just the angle that includes the subject, and the full diameter of the front element. This is the only component that's under your control at all, as the rest is part of a manufacturing process. A sun shade -- let's call it a lens hood instead, and I will explain why that's a better term in a minute -- that's properly fitted to your scope will ensure that when the lens is zoomed to its widest angle, only light that comes from the system's intended field of view gets to the front element. It's a given that when zoomed to maximum magnification, the lens shade will be somewhat less effective.
If you've been following all this, you will likely have concluded that a lens shade's effect is always desirable, unless in the case of certain photographic circumstances, that washed-out flare effect is desirable. (In a photo, you may see 'lens iris' images in such a photo, and shadow detail will be reduced, often hugely.) Unless the lens shade is too narrow for the application, it will always be a good thing. Always, since you want to be able to see the edges and shadow detail of your target as clearly as possible, even under the worst circumstances.
Now, why do I want to call this thing a lens shade, or lens hood, instead of a sunshade? Because it isn't important only in the sun. It's most important when the ambient off-axis light is significantly brighter than the light falling on your subject (or target). For example, if the sun is overhead, but somewhat forward of your position, as you aim at your target. If the sun has direct line of sight to the front glass element of your scope, you can, and will, get dramatic flare. Drama isn't what we want when we look at a target! We want to see the target!
Take the other situation, where you are in dim light, even night. If it's perf