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

For example, in the code fragment below, you could select a block of code

Extract method

and press &shortcut:ExtractMethod; (Refactor | Extract Method).

This would result in the following:

Extracted method