function
Reuse expressions in component equations and in member declarations of domains and components
Parent Section: none (top-level)
Syntax
function out = FunctionName(in1,in2) definitions out = Expression1(in1,in2); end end
Description
The function
keyword begins the Simscape™ function declaration, which is terminated by an end
keyword.
The keyword function
must be followed by the function header, which
includes the function name, inputs, and outputs.
The body of the function must be enclosed inside the definitions
block, which is terminated by an end
keyword. The
definitions
block contains equations that express the output
arguments of the function in terms of its input arguments. This block is
required.
The following syntax declares a simple function.
function out = FunctionName(in1,in2) definitions out = Expression1(in1,in2); end end
If the function has multiple return values, the syntax is:
function [out1,out2] = FunctionName(in1,in2)
Depending on whether the Simscape function is a main or local function:
Main function — You must place the function declaration in a file of the same name with a file name extension of
.ssc
. The file name must match the function name. For example, functionfoo
must be in a file calledfoo.ssc
. The file must begin with thefunction
keyword. Only blank lines and comments can precedefunction
.Local function — Include the function declaration in a component, domain, or function file, after the final
end
keyword that concludes the description of the component, domain, or main function. The local function is accessible only by that component, domain, or main function.
Syntax Rules
One or more output parameters are allowed.
If an output parameter is not used on the left-hand side of the
definitions
section, you get an error.Zero or more input parameters are allowed.
When the function is called, the number of input arguments must match the number of input parameters.
Input parameters are positional. This means that the first input argument during the function call is passed to the first input parameter, and so on. For example, if you write an equation:
o == FunctionName(5,2);
then
in1
is 5 andin2
is 2.If the function has multiple return values, they are also positional. That is, the first output parameter gets assigned to the first return value, and so on.
If the function has multiple return values, the rules and restrictions are the same as for declaration functions. For more information, see Multiple Return Values.
The
definitions
section can contain intermediate terms andif-elseif-else
statements. The same syntax rules as in the declaration section of alet
statement apply. For more information, see Using Intermediate Terms in Equations.The
definitions
section cannot contain expressions with dynamic semantics, such asinteg
,time
,der
,edge
,initialevent
, ordelay
.
Packaging Rules for Function Files
Simscape function files can reside directly on MATLAB® path or in package directories. For more information, see Organizing Your Simscape Files.
You can use source protection, as described in Using Source Protection for Simscape Files.
Importing a package imports all the Simscape functions in this package. For more information, see Importing Domain and Component Classes.
If a MATLAB function and a Simscape function have the same name, the MATLAB function has higher precedence.
Examples
Model Examples
Version History
Introduced in R2017b