Skip to main content

Command: Extract function

Function: The command opens the Extract Function Configuration dialog.

The command extracts selected code from the ST editor and creates a new method or function containing this code. The affected code in the ST editor is replaced by a correct call. When code is extracted from a function block or the child of a function block, a new method is created from the code. When code is extracted from a program or a function, a new function is created from the code.

Note also in this context: Detecting Code Clones

Call: Context menu: Refactoring

Requirements: When the selected code consists of one or more statements:

  • The selected code does not contain any compile errors.

  • The selected code is located in the implementation part of an ST POU.

  • The selected code does not contain any exiting jumps

    Examples of exiting jumps include the following:

    • Using RETURN to exit the enclosing function

    • Using CONTINUE or EXIT to exit a loop enclosing the code

Tip

You can undo the changes that the Extract function command made in your project by positioning the cursor in the device tree and clicking Edit → Undo.

Table 1. Extract Function Configuration Dialog

Name

Name of the recently created function or method

The default name can be changed.

Return value

Determines the return value of a function if there are multiple output and/or input/output parameters

Parameter

Display of the available POUs

Configuration whether the parameters are used as input, output, or input/output variables

_cds_icon_input_channel.png standard icon: Input variables

_cds_icon_output_channel.png standard icon: Output variables

_cds_icon_input_channel.png standard icon: Input/Output variables

_san_icon_revert.png

The changes made for Name, Return value, or Parameter are undone.

Upper code window

Recently created code of the call location

Lower code window

Recently created code of the function or method

OK

The displayed code changes are accepted in the ST POUs and the dialog is closed.

Cancel

The displayed code changes are rejected and the dialog is closed.