If in category to be inside of a function

I have this code in WordPress index.php and I was wondering how can I transfer this in to a functions.php to be a part of a function?

<?php if (in_category('featured')) : ?>
<a href="<?php the_permalink(); ?>">  <span  class="featured_icon">     
<img src="<?php bloginfo('template_directory'); ?>/images/featured_icon.png" />  
</span></a>                     
<?php endif; ?>

Thank You in advance for Your help 🙂

Solutions Collecting From Web of "If in category to be inside of a function"

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="<?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.