Main Content

Reuse expressions in component equations and in member declarations of domains and components

function out = FunctionName(in1,in2) definitions out = Expression1(in1,in2); end end

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, function`foo`

must be in a file called`foo.ssc`

. The file must begin with the`function`

keyword. Only blank lines and comments can precede`function`

.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.

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 and`in2`

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 and`if-elseif-else`

statements. The same syntax rules as in the declaration section of a`let`

statement apply. For more information, see Using Intermediate Terms in Equations.The

`definitions`

section cannot contain expressions with dynamic semantics, such as`integ`

,`time`

,`der`

,`edge`

,`initialevent`

, or`delay`

.

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.

Declare a function that computes the square of a sum of two numbers:

function out = SumSquared(in1,in2) definitions out = in1^2 + 2*in1*in2 + in2^2; end end

Save the function in a file named `SumSquared.ssc`

, on the
MATLAB path.

This component calls the `SumSquared`

function to compute the square
of a sum of its parameters `p1`

and `p2`

.

component MyComp outputs o = 0; end parameters p1 = 5; p2 = 2; end equations o == SumSquared(p1,p2); end end

For a more detailed example of declaring and using a main Simscape function, see the Simscape Functions example.

For a detailed example of declaring and using a local Simscape function, see Local Simscape Functions.