In Drupal 8, sometime we need to write custom code and output of this custom code to be sent to frontend templates for rendering. Here, we can write the custom code in module but I am writing custom code in *.theme file which has one mytheme_preprocessor function and I want to send a value for rendering in template.
Here is the example
function mytheme_preprocess(&$variables) {
$variables['myname'] = 'Jone Doe';
}
And you can access this value in twig file like given below
{{myname}}