You should add the functions in the User Code section:
/* USER CODE BEGIN 0 */
(..)
/* USER CODE END 0 */
Ensure that following option is checked : "Keep User Code when re-generating". In this case, the user sections defined by STM32CubeMX are preserved.
I recommend you to have a look at the UM1718 to show how to add the user code in different sections:
- B.1 STM32CubeMX generated C code and user sections
- 14.7 Updating the project with the user application code"
Note that when upgrading to a new version of STM32CubeMX, make sure to always backup your projects before loading the new project (especially when the project includes user code).
I hope that this brings some help to you.
Imen