A new method on constructing analytical potential energy functions is presented, and from this a analytical potential energy function applied to both neutral diatomic molecules and charged diatomic molecular ions is obtained. In this paper, the potential energy function is examined by 21 examples of eight different basic kinds of diatomic molecules or ions--homonuclear ground-state for neutral diatomic molecule Na2-X1 sigma g+, homonuclear excitation-state for neutral diatomic molecule C2-A1 pi(u), homonuclear ground-state for charged diatomic molecular ion He2+ -X2 sigma u+, homonuclear excitation-state for charged diatomic molecular ion N2+ -B2 sigma(u), heteronuclear ground-state for neutral diatomic molecule NaLi-X1 sigma g+, heteronuclear excitation-state neutral diatomic molecule BH-B1 sigma+, heteronuclear ground-state for charged diatomic molecular ion (BC)- -X3 pi, and heteronuclear excitation-state for charged diatomic molecular ion (CS)+ -A2 pi etc. The theoretical values of the vibrational energy level of molecules calculated by the potential energy function are compared with RKR (Rydberg-Klein-Rees) or experimental data, and as a consequence, all the results are precisely consistent with RKR data.