Show sidebar only to author of post

The reason this is not working is because you are trying to use undefined variables, they are declared outside the function and can therefor not be used without prefixing them with the global keyword.

If you have error_reporting enabled you would see warnings like this:

Notice: Undefined variable: current_user in
/… on line 11

Notice: Undefined variable: author_id in
/… on line 11

You enable debugging by setting WP_DEBUG to true in your wp-config.php file.

Adding the global keyword would make the variables accessible from within the function:

function showSidebarWhenNeeded()
{
  global $current_user;
  global $author_id;
  if ( $current_user->ID !== $author_id ) {
    // ...
  }
}

I would instead set the variables inside the function:

function showSidebarWhenNeeded($post_id)
{
  $author_id = get_post_field ( 'post_author', $post_id );
  $current_user = wp_get_current_user();
  if ( $current_user->ID !== $author_id ) {
    // ...
  }
}