{"id":4323,"date":"2014-12-11T08:00:44","date_gmt":"2014-12-11T13:00:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/seth\/?p=4323"},"modified":"2014-12-10T16:19:57","modified_gmt":"2014-12-10T21:19:57","slug":"simulink-functions-intro","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/simulink\/2014\/12\/11\/simulink-functions-intro\/","title":{"rendered":"Simulink Functions hit the big time"},"content":{"rendered":"<p>The release of R2014b brought a time-honored Stateflow feature up to the model level: <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/slref\/simulinkfunction.html\">Simulink Functions<\/a>. These blocks, analogous to stand-alone MATLAB functions, promote modularity and reuse by allowing you to call the same set of blocks from anywhere in your model \u2013 even from inside a Stateflow chart!<\/p>\r\n\r\n<p><strong>A block unlike any other<\/strong><\/p>\r\n<p>Simulink Functions are unlike any other Simulink block, even other user-defined functions like Function-Call Subsystems. For starters, the block does not use traditional ports. Instead, like MATLAB functions, you specify arguments.<\/p>\r\n\r\n<p>Inside the Simulink Function, arguments are represented by unique-looking Argument port blocks.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/argin_argout.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-4324\" alt=\"Argument Inport and Argument Outport\" src=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/argin_argout.png\" width=\"257\" height=\"86\" \/><\/a><\/p>\r\n\r\n<p>Since they aren\u2019t traditional ports, you won\u2019t find these blocks in the Library Browser. To add or remove arguments, you type right on the block\u2019s mask:<\/p>\r\n\r\n<p><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blogs.mathworks.com\/images\/seth\/2014Q4\/SimulinkFunction.gif\" width=\"600\" height=\"328\" alt=\"How to specify Simulink Function arguments\" class=\"alignnone\" \/><\/p>\r\n\r\n<p>To use a Simulink Function in your model, you employ a Function Caller block.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCallers.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-4325\" alt=\"You can also still call Simulink Functions from a Stateflow chart\" src=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCallers.png\" width=\"600\" height=\"568\" \/><\/a><\/p>\r\n\r\n<p>Simulink associates each instance of this block with a Function by the function name; it requires that every function name in the model hierarchy be unique.<\/p>\r\n\r\n<p><strong>Functions in referenced models<\/strong><\/p>\r\n<p>Speaking of hierarchy, one very practical use of Simulink Functions are in a referenced model context. Functions defined in a referenced model are in the scope of its parent. But, the referenced model has to follow some very special design rules, <a href=\"https:\/\/www.mathworks.com\/help\/simulink\/ug\/export-function-models.html#bt170o9\">the same as they do when they contain Function-Call Subsystems<\/a>.<\/p>\r\n\r\n<p>The simplest way to adhere to these rules is to create a model that contains nothing but your collection of Simulink Function blocks, like so:<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCollection.png\"><img decoding=\"async\" loading=\"lazy\" width=\"500\" height=\"291\" class=\"alignnone size-full wp-image-4326\" alt=\"functionCollection\" src=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCollection.png\" \/><\/a><\/p>\r\n\r\n<p>Now add a Model block to your parent model, point it to this collection, and you can call any of these functions from the parent, or from any other model further up the hierarchy.<\/p>\r\n\r\n<p><a href=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCallersFromRef.png\"><img decoding=\"async\" loading=\"lazy\" width=\"600\" height=\"453\" class=\"alignnone size-full wp-image-4327\" alt=\"Function names throughout the model hierarchy must be unique\" src=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCallersFromRef.png\" \/><\/a><\/p>\r\n\r\n<p><strong>Generating code <\/strong><\/p>\r\n<p>Code generated from Simulink Functions is as modular and straightforward as you would expect. Each function is defined in its own source file. The function prototype always takes the form of:<\/p>\r\n\r\n<p><code>void foo(type_T in1, type_T in2, type_T *out1, type_T *out2)<\/code><\/p>\r\n\r\n<p>Simulink is smart enough to recognize when you are trying to emulate pass-by-reference, and you name your output the same as your input (like I did in the <tt>timestwo<\/tt> example in the images above). When you do, the generated function prototype looks just like this:<\/p>\r\n\r\n<p><code>void foo(type_T *x)<\/code><\/p>\r\n\r\n<p><strong>Now it\u2019s your turn<\/strong><\/p>\r\n<p>This opens up a whole new way to model. What kind of design patterns can you create?<\/p>","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/seth\/files\/feature_image\/functionCallersFromRef.png\" onError=\"this.style.display ='none';\" \/><\/div><p>The release of R2014b brought a time-honored Stateflow feature up to the model level: Simulink Functions. These blocks, analogous to stand-alone MATLAB functions, promote modularity and reuse by... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/simulink\/2014\/12\/11\/simulink-functions-intro\/\">read more >><\/a><\/p>","protected":false},"author":88,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[21,65,66,30,56,16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4323"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/users\/88"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/comments?post=4323"}],"version-history":[{"count":15,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4323\/revisions"}],"predecessor-version":[{"id":4344,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/posts\/4323\/revisions\/4344"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/media?parent=4323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/categories?post=4323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/simulink\/wp-json\/wp\/v2\/tags?post=4323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}