System Objects are designed specifically for implementing and simulating dynamic systems with inputs that change over time and System objects use internal states to store that past behavior, which is used in the next computational step which is applicable in your case. Therefore, I suggest you use System Objects rather than functions. And MATLAB System Block will enable you to use System Objects in Simulink. Please refer to the links to know more about System Objects and System block.
https://www.mathworks.com/help/matlab/matlab_prog/what-are-system-objects.htmlhttps://www.mathworks.com/help/simulink/ug/what-is-matlab-system-block.html
Hope this helps.