I don’t know where in WP you define your custom functions. I believe it’s in functions.php, yes. As long as the function is defined somewhere where it will be available when you try to use it, you’re fine.
You would use this function in your code above instead of printing out the post_mime_type directly:
// instead of
// you would use
echo friendly_mime( $attachment->post_mime_type );