The primary purpose of fruit that develops on flowering plants is protection and dispersal of seed.
A fruit is a seed container that develops from an ovary and any of the tissues that surround it. Fruits are products of flowers and will therefore be found only in flowering plants.
The fruit protects the developing seed and aids in the dispersal of fully developed seeds which is accomplished by animals when they are attracted the fruit and eat it, walk off and defecate out the seeds elsewhere.
Fruits that develop from other flower parts apart from the ovary are called accessory fruits (pomes) e.g. melons and squash