Documentation

function

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

Syntax

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

Description

function keyword begins the Simscape™ function declaration, which is terminated by an end keyword. Only blank lines and comments can precede function. You must place a function declaration in a file of the same name with a file name extension of .ssc.

The keyword function must be followed by the function header, which inludes the function name, inputs, and outputs.

The body of the function must be enclosed inside the definintions block, which is terminated by an end keyword. The definintions 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)

Syntax Rules

  • The file name must match the function name. For example, function foo must be in a file called foo.ssc.

  • One or more output parameters are allowed.

  • If an output parameter is not used on the left-hand side of the definintions section, you get an error.

  • Zero or more input parameters are allowed.

  • When the function is called, the number of input argulments 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 definintions 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 definintions section cannot contain expressions with dynamic semantics, such as integ, time, der, edge, initialevent, or delay.

Packaging Rules

Examples

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 Simscape function, see the Simscape Functions example.

Introduced in R2017b

Was this topic helpful?