CLion 2018.1 Help

Extract function


When the Extract Function refactoring is invoked, CLion analyses the selected block of code and detects variables that are the input for the selected code fragment and the variables that are output for it.

The detected output variable is used as a return value for the extracted function.

C++ example

cl extractMethodBefore
cl extractMethodAfter

Extracting a function

To extract a function, follow these steps:

  1. In the editor, select a block of code to be transformed into a function.
  2. On the main menu or on the context menu of the selection, choose Refactor | Extract | Method or press Ctrl+Alt+M.
  3. In the Extract Function dialog box that opens, specify the name of the new function.
  4. Specify the return type of the function.
  5. If the function has not been yet declared, select declaration place: above or below current place.
  6. In the parameters pane:
    • Add the new parameters or delete the unnecessary ones
    • Rename parameters or/and change their types by clicking the corresponding parameter lines and entering the new names and types
    • Reorder parameters in the list
    • Change the return type of the function
  7. Check the result in the Signature Preview pane and click Extract to create the function.

    The selected code fragment will be replaced with a function call.

    cl extractMethodDialog
Last modified: 24 July 2018

See Also

External Links: