Motivated by the widely discussed Ag doped ZnO and the lack of follow-up reports about the realization of p-n junctions, we calculated the electronic structures and optical properties of pure, Ag-doped and Ag-F codoped ZnO based on the density-functional theory. It was found that Ag doped ZnO shows p-type conduction character. But there are some unstable factors and self-compensations in this structure. We also calcualted the formation energy and ionization energy of the impurity for Ag-F codoped ZnO. It was found that incorporating the reactive donor F into Ag doped ZnO system, not only enhances the Ag acceptor solubility, but also gets a shallower Ag acceptor energy level in the band gap. In addition, we analyze the imaginary part of the dielectric function, reflectivity and absorption coefficient for pure ZnO and Ag-F codoped ZnO. Compared with the pure ZnO, the remarkable feature for Ag-F codoped ZnO is that there is a strong absorption in the visible-light region, which indicates that it could be taken as a potential candidate for a photocatalytic material.