The primary function of sunscreen is to prevent damage to the epidermis and dermis layers of the skin. Without protection, UV radiation can cause DNA damage in keratinocytes in the epidermis, leading to mutations and potentially skin cancer. In the dermis, UV exposure can degrade collagen and elastin fibers, affecting the skin's structural integrity and elasticity.