%% Generate Predictable Function Interface to Match Graphical Block Interface
% You can generate code for nonreusable Subsystem that has a predictable
% function interface. A predictable interface can be useful for debugging and
% testing your code and integrating it with external applications. The generated code
% with a predicatable function interface is more readable. 
%
% When you specify block parameter *Function interface* as |Allow arguments
% (Match graphical interface)|, the generated function interface matches
% the Subsystem's graphical block interface. For example, if a
% Subsystem has two Inports and one Outport, the function interface
% generates two arguments for the Inports and one function argument for the
% Outport. The order of the arguments is determined by the port index.
% The function interface is predictable and does not change. 
%
% When you generate code for nonreusable Subsystem with the block
% parameter *Function interface* set to |Allow arguments (Optimized)|, the
% code generator generates a Subsystem function interface that passes data
% through arguments. The function arguments might not match with the
% Subsystem's graphical block interface.
%
% This example shows how the generated code is different when you specify 
% *Function interface* as |Allow arguments (Optimized)| and |Allow arguments
% (Match graphical interface)|.

% Copyright 2020 The MathWorks, Inc.

%% Generate Function Interface That Has Optimized Arguments
% 1. Open the example model |ex_mux_allow_arguments|. The Subsystem in the model
%    contains three inputs and two outputs.
open_system('ex_mux_allow_arguments')
%%
% 2. Right-click the Subsystem and select *Block Parameters
%    (Subsystem)* from the context menu. Alternatively, click in the Subsystem 
%    to open the Block Parameters dialog box in the *Property Inspector* 
%    pane.
%
% 3. Observe these preset settings that are needed to generate expected code:
%
% * In the *Main* tab, the block parameter *Treat as atomic
%    unit* is selected. 
%
% * In the *Code Generation* tab, *Function packaging* is |Nonreusable function|
%    and *Function interface* is |Allow arguments (Optimized)|.
%
% 4. Open the *Embedded Coder* app. Click *Build*.
evalc('rtwbuild(''ex_mux_allow_arguments'')');

%%
% The generated code in |ex_mux_allow_arguments.c| file contains a Subsystem 
% function interface with arguments:
file = fullfile('ex_mux_allow_arguments_ert_rtw','ex_mux_allow_arguments.c');
rtwdemodbtype(file,'/* Output and update for atomic system', ...
    '/* Model step function */',1,0);
%%
% The |Allow arguments (Optimized)| specification leads to optimized 
% generated code by reducing global RAM. The function interface
% has some inputs and outputs as function arguments. The generated
% function interface does not match the Subsystem's graphical block
% interface.

%% Generate Function Interface That Has Arguments that Match Graphical Interface
% 1. In the example model |ex_mux_allow_arguments|, open the Subsystem
%    Block Parameters dialog box.
%
% 2. On the *Code Generation* tab, specify *Function interface* as |Allow 
%    arguments (Match graphical interface)| and click *Apply*.
set_param('ex_mux_allow_arguments/Subsystem','FunctionInterfaceSpec',...
    'Allow arguments (Match graphical interface)')
%%
% 3. Generate code for the model.
evalc('rtwbuild(''ex_mux_allow_arguments'')');

%%
% The generated code in |ex_mux_allow_arguments.c| file contains a Subsystem 
% function interface with arguments that match the Subsystem block interface:
file = fullfile('ex_mux_allow_arguments_ert_rtw','ex_mux_allow_arguments.c');
rtwdemodbtype(file,'/* Output and update for atomic system', ...
    '/* Model step function */',1,0);
%%
% The |Allow arguments (Match graphical interface)| specification generates
% a predictable interface and maps the function arguments with the
% graphical interface of the Subsystem block. There are arrays
% as part of the function arguments to account for the combination of input or
% output signals that th Mux block uses.
%
% For the Enabled, Triggered, or Resettable Subsystem with |Allow arguments 
% (Match graphical interface)| specification, the generated function contains arguments
% corresponding to each Enable, Trigger block, and/or Reset block present within that
% Subsystem.

%% Limitations
% * The *Function interface* set as |Allow arguments (Match graphical interface)|
%   specification is not applicable to context menu option *C/C++* > *Build 
%   this Subsystem*.
% * You cannot control the argument names.
