How to change the output of a theme function? I know this question already has some "kind" of answer in documentations but they are too abstract for me. Most documentation I have seen on Drupal doesn't explain all the details of how to properly override a theme function, or maybe I don't understand.

My problem is that I need to change the output of theme function theme('links__system_main_menu', ...), which is used in page.tpl.php to print the content of the main menu. I don't understand how to make it done. Please explain me every step you take and why did you take that step.
To override the output of a theme function in a theme, you simply take the name of your theme, and the name of the theme function, and join them together with an underscore.

So for your example, the function would be called:

function MYTHEME_links__system_main_menu($variables) {
  $html = '&lt;ul&gt;';

  // Loop over links

  $html .= '&lt;/ul&gt;';

  return $html;
}


Your function should return the HTML string that you want to be rendered when this theme is used, any variables that have been passed to the theme() function will be available to you in $variables.

Usually it's enough to look at the original theme function to find out what's available to build your HTML, but sometimes you'll need to get the Devel module installed and inspect vars with dpm to properly see what's going on.