Bayonets were originally invented for hunting boars and bears. Hunters in the Pyranees in the matchlock days carred huge knives, because there was no chance for a reload when a wounded critter charged.
Then someone woke up and smelled the coffee -- if you're close enough to the bear or boar to use your knife, he's close enough to use his weapons.
So someone designed a knife with a tapered, plug-shaped handle. When you fired, you quickly slapped that plug handle down the muzzle and you had a spear.
These things were first made in a little manufacturing town, Bayonne, France.