How can I add custom markup with a theme function? I have a simple form as follows

function MY_MODULE_form($form, &amp;$form_state) {
  $form['MY_MODULE']['message'] = array(
    '#type' =&gt; 'markup',
    '#markup' =&gt; 'My Heading',
    '#tree' =&gt; true,
  return $form;
}


But I want the markup to change depending on some factor which will be defined inside a function.

So how would I call a function to do this for the #markup ?
To do this the drupal way, you should use a theme function:

function MY_MODULE_form($form, &amp;$form_state) {
  $form['MY_MODULE']['message'] = array(
    '#theme'    =&gt; 'mymodule_markup',
    '#var1'     =&gt; $var1,
    '#var2'     =&gt; $var2,
  );
  return $form;
}


Then implement hook_theme:

function MY_MODULE_theme() {
return array(
  'mymodule_markup' =&gt; array(
    'variables' =&gt; array('var1' =&gt; NULL, 'var2'=&gt;NULL),
  ),
);


}

Then implement your theme function

function theme_mymodule_markup($var1, $var2) {
    return 'some string';
}


You also could use a template to render your markup. That way themers can overwrite your markup without having to dig into your module files.