Cylinders are used in both steam locomotives and diesel locomotives, but for different processes. In the mechanical engineering sense, inside a cylinder there is usually a piston reciprocating (moving back and forth).
Steam Locomotive[]
The cylinders in a steam locomotive is used to contain steam from the boiler. Steam is fed into one side of the piston and expands, pushing the piston in the opposite direction. When the expansion ends, the steam is then added to the other side of the piston, pushing the exhausted steam out through a valve.
Diesel Locomotive[]
Diesel locomotives have an internal-combustion motor inside, to propel them. This has cylinders inside it. Fuel is ignited in a compartment called the combustion-chamber, made by the cylinder head, sides and piston head. This ignition creates energy that pushes the piston down the cylinder creating movement.
Pumps[]
In many types of locomotives, there are pumps that move fluids around. These are usually made from cylinders, with pistons reciprocating inside them to pump the fluid along.