If you want to use the function to output the code somewhere:
function displayImage($currentPost) {
// Show the featured icon only if current post is in "featured" category
if ( in_category ( 'featured', $currentPost ) ) {
$output="<a href="https://wordpress.stackexchange.com/questions/58811/<?php the_permalink(); ?>">
<span class="featured_icon">
<img src="<?php bloginfo("template_directory'); ?>/images/featured_icon.png" />
</span>
</a>';
}
else { $output="" };
return $output;
}
Then you can just pass the post id and echo the function.