How to create a contact form using FAPI in a custom module?
When dealing with Form API in Drupal 7 there are two major steps:

Step 1: Create and implement the form in a module

In this step 1, you will setup the form, and then validate and submit the form, as further detailed below.

a) Setup the form

    function form_example() {
       $form['first_name']=array(
       '#type' =&gt; 'textfield',
       '#title' =&gt; t('First name'),
       '#description' =&gt; t('Enter first name'),
        );
        $form['last_name']=array(
        '#type' =&gt; 'textfield',
        '#title' =&gt; t('Last name'),
        '#description' =&gt; t('Enter last name'),
        );
    $form['submit_button'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; t('Click Here!'),
        );

    return $form;
    } 

    function form_example_validate($form, &amp;$form_state) {

    }
    function form_example_submit($form, &amp;$form_state) {

    }


b) Validate and submit the form

Now the form is ready to be displayed.

One way to display the form, is to theme the form in the module and then print the form.

function mytheme_theme($existing, $type, $theme, $path) {
   'theme_contact_form' =&gt; array (
        'variables' =&gt; array(
            'contact_form' =&gt; FALSE,
            ),
        'template' =&gt;'theme-contact-form',
    ),
 }
function mytheme_block_info() {
   $blocks['block_contact_form'] = array(
      'info' =&gt; t('My Theme Contact form'),
      );
   return $blocks;
}
function mytheme_block_view($delta = '') {
  $block = array();
  switch ($delta) {
  case 'block_contact_form':
        $block['content'] = array(
            '#theme' =&gt; 'theme_contact_form',
            '#contact_form' =&gt; drupal_get_form('form_example'),
            );
        break;
  }
  return $block;
}


Then create theme-contact-form.tpl.php in the module file and print the form:

&lt;?php print render($contact_form); ?&gt;


With all previous steps complpeted, the form is created and implemented in the module. 

Step 2: style the form

To style the form from theme layer, refer to How to a use a template file to theme a form?