Writing to a file or a string with the same function

I generally try to adhere to the single responsibility principle. The procedure you show violates that principle by doing formatting and output. Thus, in my projects I have procedures that do formatting and only produce strings. Where those strings go is a decision made elsewhere.