How to set a taxonomy term programmatically for a node? I created taxonomy terms and assigned it to a content type 'Article' using term reference field. I created nodes by programmatically but how to set taxonomy terms for the nodes?

The taxonomy terms are created already. I want to set the taxonomy term for a node programmatically.
Use this function to get ID from Termname

function _get_tid_from_term_name($term_name) {
  $vocabulary = 'tags';
  $arr_terms = taxonomy_get_term_by_name($term_name, $vocabulary);
  if (!empty($arr_terms)) {
    $arr_terms = array_values($arr_terms);
    $tid = $arr_terms[0]-&gt;tid;
  }
   else {
    $vobj = taxonomy_vocabulary_machine_name_load($vocabulary);
    $term = new stdClass();
    $term-&gt;name = $term_name;
    $term-&gt;vid = $vobj-&gt;vid;
    taxonomy_term_save($term);
    $tid = $term-&gt;tid;
  }
  return $tid;
}