Lots of plants have glands that secrete nectar, the sweet liquid so attractive to bees and other insects. However, these glands aren’t always in the flower. When they’re outside the flower, they’re called extrafloral nectaries.
The function of these extrafloral nectaries has been conjectured for years and years, but still no one is really certain what their role is. Back when Charles Darwin was writing On the Origin of Species he noted that:
Certain plants excrete sweet juice, apparently for the sake of eliminating something injurious from the sap. … This juice though small in quantity is greedily sought by insects; but their visits do not in any way benefit the plant.
This remains one of the hypotheses for the role of the extrafloral nectaries – that they are helping with them elimination of undesirable compounds.
The other hypothesis, which is more popular these days, is that the extrafloral nectaries attract beneficial insects which help in defending the plant from “pests”.
I have to say, in my experience the glands attract mainly ants who, as Darwin noted, feed greedily from them. Meanwhile the aphids still come and begin feeding on the plant leaves. Then the ants, which are there in abundance having been dining at the extrafloral restaurants, set up their aphid farms, defending the aphids from would be predators like ladybirds. The ants then live a particularly happy life, lounging drunkenly around the extrafloral nectaries and milking their aphids.
There must be more to this than meets the eye though, otherwise it would be a bit of a suicidal strategy.