The Drupal CAPTCHA module provides a great configuration page for adding captchas to webforms and forms created with the Drupal form API (created in code). I recently encountered a situation with a custom, multi-page form that needed a CAPTCHA. The problem was that the CAPTCHA was showing up on the first page of the form, which seemed like a strange user experience. Fortunately, it is possible to insert a CAPTCHA into a FAPI form in code as well.
The #captcha_type will define which style of CAPTCHA will be displayed. ( Image and reCAPTCHA require that those modules are enabled in addition to the regular CAPTCHA module )
$form['captcha_fieldset'] = array( // wrapping in a fieldset so it looks better
'#type' => 'fieldset',
'#title' => 'CAPTCHA',
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'my_form_captcha' => array(
'#type' => 'captcha',
'#captcha_type' => 'captcha/Math' // displays the default math captcha
// '#captcha_type' => 'recaptcha/reCAPTCHA' // requires the reCAPTCHA module
// '#captcha_type' => 'image_captcha/Image', // requires image_captcha module (distributed with the captcha module)
)
);