Mr. Bowker said his clients neither promoted prostitution nor approved of it. “They would like to be free to engage in this important discussion,” Mr. Bowker said of his clients, “and to be unfettered by a policy requirement that demands fealty to the government’s viewpoint.”

Sri Srinivasan, a deputy solicitor general whose nomination to the United States Court of Appeals for the District of Columbia Circuit is pending, gave the justices several guideposts. The conditions must be germane, he said. Using government money to suppress a viewpoint is not permissible, but the government should be free to choose between groups that share its policy goals and those that oppose them.

Several justices tested those limits with questions about higher education, recycling and teenage pregnancy. Justice Alito, perhaps the most skeptical of the law, asked whether groups receiving money under a health care law could be made to take a stance on the availability of guns.

Mr. Srinivasan said that if “the requisite germaneness” were satisfied and the government’s goal was not to suppress disfavored speech, such a condition might be permissible. He seemed to make headway by characterizing the required pledge as a selection criterion rather than an effort to force groups to adopt views they did not hold.

Echoing the point, Chief Justice Roberts asked Mr. Bowker why the government would “want to sign up with somebody who didn’t share the objectives of the program.” Mr. Bowker responded that it would be problematic to deny financing based on a group’s viewpoint.

Justice Antonin Scalia disagreed. “They can’t fund the Boy Scouts of America because they like the programs that the B.S.A. has?” Justice Scalia asked. “They have to treat them equivalently with the Muslim Brotherhood? Is that really what you’re suggesting?”

Mr. Bowker said the government had not articulated a meaningful limiting principle.

“On the government’s theory,” he said, “the government can give anyone in the country a dollar in Medicare funds and say, O.K., now that you’ve taken a dollar of our money, we want you to profess your agreement with the Affordable Care Act, and we want you to never say anything inconsistent with that in your private speech.”

Justice Elena Kagan recused herself from the case — United States Agency for International Development v. Alliance for Open Society International, No. 12-10 — presumably because she had worked on it as United States solicitor general.