First create a function that returns the variable.
Then you must create a preprocess function, for the template, like this:

    function MY_MODULE_preprocess_TEMPLATE(&$variables) {
      $V_NAME = my_function();
      $variables['v-name'] = $V_NAME;
    }

Remember to clear the cache.