Plants primarily secrete nectar as an energy source to tempt pollinators to visit their flowers, but the secretion of this substance appears to have evolved long before flowering plants appeared. Many plants, including some ferns, secrete nectar from extrafloral nectaries - i.e. nectaries in other positions on the surface of the plant.
Legumes, like the common vetch Vicia sativa in the image above, have extrafloral nectaries on their stipules (the small, leaf-like projections on either side of the base of a leaf stalk). The extrafloral nectary is the black spot on the image above and a closer look ....
... reveals what its function might be. Ants are famous for their attraction to sweet substances and regularly visit the plant for the sugar that leaks out of these locations. This might deliver two kinds of selective advantage to the plant that would outweigh the cost of using some of its assimilated sucrose in this way. In some plants it might deflect ants, which are usually very inefficient pollinators, away from the larger source of nectar that's there to service more efficient pollinators, like bees. In other plants it may be a way of recruiting a defensive army of ants because they become aggressive towards herbivorous insects that might try to plunder their food supply; in Acacia trees for example, the defensive benefits of hosting ants are well documented.
Extrafloral nectaries are found in a wide variety of plants and are often located on leaf petioles and mid-ribs. This is a vertical section through an extrafloral nectary on the underside of the mid-rib of a cotton plant (Gossypium sp.), stained with fluorescent dyes. The bright yellow cells at the top are xylem vessels, conducting water to the leaf blade. The very small, brick shaped blue cells below are dividing cambial cells and also phloem sieve elements that are conducting assimilated sucrose away from the leaf blade. Below that are some larger, blue-stained parenchymatous cells and then, at the very bottom, there are thin-walled finger-shaped cells which constitute the extrafloral nectary tissue, on the lower surface of the leaf mid-rib.
The blue staining is due to cellulose in the cell walls binding to a dye called calcofluor, which then fluorescence blue in UV light. You can see from this image that there's a very thin cellulose cell wall in those finger-shaped extrafloral nectary cells, because they barely fluoresce. So they easily leak sucrose that accumulates in them. The other interesting feature of this section is the orange staining in the small cells immediately above those extra-floral nectary cells. This is the endoplasmic reticulum/ Golgi complex inside the cells - the membranes and secretory vesicles that manufacture substances and transport them between cells via channels in the cell walls called plasmodesmata; these brightly-fluorescing cells seem to be highly metabolically active, so maybe the nectary cells are secreting something else, as well as sucrose.
There are some scientific papers on cotton extrafloral nectaries, their role and how they might be exploited in biological control programmes in this crop here, here and here.