They (slightly) preload the bearings during assembly and discourage the left crank walking off the end after (very) long periods of time*. They also act as a dust cap.
They come in various different lengths. Older cranksets had shorter ones, but I think it’s fine to mix them. MTB and GRX ones have a hole all the way through.
(* A guy on some forum somewhere claimed the latter actually happened to people he knows)
I think you are talking about the plastic pre-load end-cap, which you do up with the plastic tool.
That has the function of setting the pre-load on the bearings, to remove any lateral play.
The thing being discussed here is the little pin thing that sits in the slot in the NDS crank, where the bolts go. It's a fairly useless item which both bolts pass through, and has a pin which sits into a hole in the spindle. I don't think it has any actual function in normal use of a properly installed crankset.
The only thing I can see it doing is preventing the crank actually falling off if the bolts come loose.