Extract Function
Basics
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
Before | After |
---|---|
Before | After |
---|---|
function MyFunction(a,b) {
c = a + b;
d = c * c;
return d;
} | function Sum(a,b) {
return a + b;
}
function MyFunction(a,b) {
c = sum(a,b);
d = c * c;
return d;
} |
Extracting a function
To extract a function, follow these steps:
- In the editor, select a block of code to be transformed into a function.
- On the main menu or on the context menu of the selection, choose or press Ctrl+Alt+M.
- In the Extract Function dialog box that opens, specify the name of the new function.
- Specify the return type of the function.
- If the function has not been yet declared, select declaration place: above or below current place.
- In the parameters pane:
- 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.
See Also
Last modified: 19 July 2017