S-functions are the most powerful way of defining custom blocks. There's no limit to what you can do: re-use an existing code, implement a functionality not available in your current blocksets, exchange data with external applications and devices, and so on. If you can code it, you can use it.
S-functions can be implemented using the following languages: the MATLAB scripting language, C/C++, and Fortran. S-functions written with the latter two need to be compiled first in order to produce a shared library loadable by the Simulink engine, while a MATLAB S-function will be interpreted and executed during the simulation.
In this chapter we'll focus on Level 2 MATLAB S-functions, since they are the easiest to develop even for people without programming knowledge. But the final part of this chapter, aimed at those who already know the C language, will explain how to obtain the same results with C MEX S-functions.