Main Content

Function Prototyping

This example shows how to create and customize your function prototype.

C Construct

double add_function(double u1, double u2)
{ 
   return u1 + u2;
}

Function Call by Using Graphical Functions

1. Open example model ex_func_SF. This example model contains two Inport blocks and one Outport block.

The Stateflow chart contains a graphical function that you create by clicking the fx button and placing a graphical function into the Stateflow chart.

The graphical function contains this signature:

output = add_function(u1, u2)

In the Stateflow chart, there is an example of a simple transition that calls add_function.

2. Open the Model Explorer. From the Model Hierarchy tree, select ex_func_SF > Chart > add_function. On the right pane, make sure that the Function Inline Option is set as Function.

3. From the Model Hierarchy tree, click Chart. On the right pane make sure that the Export Chart Level Functions parameter is selected.

4. To build the model and generate code, press Ctrl+B.

ex_func_SF.c contains the generated code:

/* Function for Chart: '<Root>/Chart' */
void add_function(real_T in1, real_T in2, real_T *output)
{
  *output = in1 + in2;
}

/* Model step function */
void ex_func_SF_step(void)
{
  /* Chart: '<Root>/Chart' incorporates:
   *  Inport: '<Root>/u1'
   *  Inport: '<Root>/u2'
   */
  add_function(u1, u2, &y1);

  /* Outport: '<Root>/y1' */
  rtY.y1_e = y1;
}

Control Function Prototype of the model_step Function

1. Open example model ex_control_step_function.

2. Open the Code perspective. In the Code Mappings editor, select the Entry-Point Functions tab.

3. In the step function row, under the Function Preview column, click the prototype hyperlink. The configuration dialog box used for customization opens. The first field in the dialog box, C function prototype, automatically updates to preview the changes to the step function as you make them.

4. Change the function name. If you do not specify a name, the code generator names the function based on a default naming rule. For this example, rename the function by setting C Step Function Name to ex_control_run. The function preview changes to reflect the new step function name.

5. Configure step function arguments. If an application requires a void-void interface, clear Configure arguments for Step function prototype. For this example, select that option.

6. Display current default settings for the entry-point function arguments by clicking Get default. An updated diagram appears.

7. Configure the function return argument by setting C return argument to void or one of the listed outport arguments. For this example, select void. Check your change in the preview.

8. Configure the function input and output arguments. For each root inport and outport, you can change the C type qualifier and argument name. For details, see Configure Name and Arguments for Individual Step Functions.

For this example:

  • For u1, set C Type Qualifier to Value and C Identifier Name to arg_u1.

  • For u2, set C Type Qualifier to Pointer to const and C Identifier Name to arg_u2.

  • For the outport, set C Identifier Name to arg_y1.

Review your changes in the preview.

9. To validate your changes, click Validate.

10. Apply your changes. Click Apply. Then, click OK.

11. To build the model and generate code, press Ctrl+B.

ex_control_step_function.c contains the generated code:

void ex_control_run(real_T arg_u1, const real_T *arg_u2, real_T *arg_y1)

Related Topics