The primary function of a Layer 3 switch is to forward data packets between different networks or subnets. Layer 3 switches operate at the network layer of the OSI model and can perform routing functions similar to traditional routers. They use IP addresses to make routing decisions and can efficiently route traffic between different VLANs (Virtual Local Area Networks) or subnets within the same network. This helps in optimizing network performance and reducing data congestion by directing traffic based on destination IP addresses.