The Extract Function refactoring creates a function out of a code fragment. Select a block of code and press &shortcut:ExtractMethod; (Refactor | Extract | Function).

Extract method

This will result in the following:

Extracted method