For using common function, the best approach is to create a custom module and use Magento 2 Helper which gives you one of the best opportunities to override core files and pre-defined functions of the Magento 2 platform.

The Magento 2 helper is the class which is used for adding functionalities to several features and can be used anywhere in the entire website. They are created as Singleton (single instances of objects) which can be called in controllers, views, models, templates etc.

For example, You can use the above code in your helper and inject your helper class anywhere in the code to get the data from the code.

For more detailed information you can visit this [link][1].


  [1]: https://magenticians.com/using-magento-2-helper/

**Hope it helps!!!**