How do we know we should move code inside a function?

When there's lots of duplicated code doing the same thingWhen there's lots of lines of codeWhen we're creating a lot of variables

When there's lots of duplicated code doing the same thing.