Can't understand how to invoke my own theme() function i have made formexample.module, from Drupal 7 Pro Development

function formexample_menu() {
    $items['formexample'] = array(
        'title' =&gt; 'View the sample form',
        'page callback' =&gt; 'drupal_get_form',
        'page arguments' =&gt; array('formexample_nameform'),
        'access callback' =&gt; TRUE,
        'type' =&gt; MENU_NORMAL_ITEM,
    );
    return $items;
}

function formexample_nameform() {
    $form['name'] = array(
        '#title' =&gt; t('Your name'),
        '#type' =&gt; 'fieldset',
        '#description' =&gt; t('what people call you'),
    );

    $form['name']['user_name'] = array(
        '#title' =&gt; t('Your name'),
        '#type' =&gt; 'textfield',
        '#description' =&gt; t('please enter name'),
    );

    $form['submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; t('Submit'),
    );
    return $form;
}


and a theme function and preprocess function

function formexample_theme() {
    return array(
        'formexmaple_nameform' =&gt; array(
            'render element' =&gt; 'form',
            'template' =&gt; 'formexample-nameform',
        ),
    );
}

function template_preprocess_formexample_nameform(&amp;$variables) {
    $variables['name'] = $variables['form']['name'];
    $variables['submit'] = $variables['form']['submit'];
}


this is template file for this theme() - formexample-nameform.tpl.php

&lt;div id="formexample_nameform"&gt;
&lt;?php
    print render($name);
    print render($submit);
?&gt;
&lt;/div&gt;


my question is: how and where to invoke this theme('formexample_nameform') , i thought i should insert function template_preprocess_formexample_nameform to my template.php file(with template rename to my theme name) but its not working for me
You just need to set it on the $form element inside your form function:

function formexample_nameform($form, &amp;$form_state) {
  $form['#theme'] = 'formexmaple_nameform';
  // ...


When you use render element with a theme the variable on which you set #theme is the one that's passed through to the theme/preprocess functions.

Don't forget to add drupal_render_children() at the bottom of your template file or the form won't render correctly:

echo drupal_render_children($form);