No. Your primary function is what you HAVE to do. There is never a time that cataloging gaseous anomalies and star charting takes precedence over defense. "The Romulans are attacking our outposts." "But SCIENCE!" "Ah. your primary purpose. Very good then. Carry on."
I would disagree with that on a fundamental level. A primary purpose is... a primary purpose. As person, my "primary purpose" is to work and pay taxes? I have to do it, but... I would disagree that it's my primary purpose.
Rather, I would argue the exact opposite. Yes, defense is something Starfleet has to do. It doesn't particularly want to. It wants to engage in it's primary purpose of seeking out new worlds and new civilizations. It just... has to do defense when necessary.
It's like flight attendants. Most of the time they serve drinks and meals. But their PRIMARY purpose is to get passengers off the plane in emergencies.
That's also objectively incorrect. A flight attendants primary purposes is to... serve the passengers and generally run the cabin. An additional duty they have when the need arises is to get passengers off the plane in emergencies, which if/when that occurs, BECOMES their primary duty for the duration of the emergency.
Maybe that is a better way to think about Starfleet? Defense isn't its primary function, until it has to be. When the situation arises, their primary function IS defense. When the need lessens, they revert to their previous primary function of science and exploration.