If you look at the photographs of submarines from the Second World War, you can see one curious detail. Many of them have one (rarely several) cannons or machine guns installed directly on the hull. The question arises: why are they even needed, if the submarine moves under water, and the weapons presented on the outside are clearly not "connected" to the internal systems of the submarine?
Everyone knows that the submarine fires torpedoes first! Especially when it comes to submarine warfare during the Second World War. In fact, this is far from the case. The thing is that the primary task of any submarine fleet in the event of war (perhaps with the exception of modern missile-carrying submarines) is to hunt for transport ships. Submarines should primarily damage the economy and enemy supply lines.
Therefore, all the catch of the submarine can be divided into large and small "fish". The most dangerous or heavily sunk ships of the submarine during the Second World War were fired exclusively with torpedoes from a submerged position. However, not all transport ships had at least some kind of protection. Often, the transport workers went either in small weakly protected groups or even alone.
Thus, it turned out that there was no point in spending precious torpedoes on the sinking of a small vessel - "small fish". For this, artillery pieces were needed on board, right in front of the wheelhouse. With their help, the crew of the submarine could well sink a small transport from the surface position. The same guns could be used for emergency protection, for example from torpedo boats.
This was extremely important, since the submarine spent a significant part of the time in the roadstead on the surface (this way less fuel is consumed). At the same time, the submarine could easily be taken by surprise and it did not always have the opportunity to quickly go under water.
Continuing the topic, read how the Soviet submarine B-307 was able to in the middle of the Volga steppes.