A characterization of convex functions