In this paper, a switch function-based gas-kinetic scheme (SF-GKS) is presented for the simulation of inviscid and viscous compressible flows. With the finite volume discretization, Euler and Navier-Stokes equations are solved and the SF-GKS is applied to evaluate the inviscid flux at cell interface. The viscous flux is obtained by the conventional smooth function approximation. Unlike the traditional gas-kinetic scheme in the calculation of inviscid flux such as Kinetic Flux Vector Splitting (KFVS), the numerical dissipation is controlled with a switch function in the present scheme. That is, the numerical dissipation is only introduced in the region around strong shock waves. As a consequence, the present SF-GKS can well capture strong shock waves and thin boundary layers simultaneously. The present SF-GKS is firstly validated by its application to the inviscid flow problems, including 1-D Euler shock tube, regular shock reflection and double Mach reflection. Then, SF-GKS is extended to solve viscous transonic and hypersonic flow problems. Good agreement between the present results and those in the literature verifies the accuracy and robustness of SF-GKS.