When is it a good idea to create a function?

When we need to use the same lines of code multiple timesWhen we need to create a variable

It's a good idea to write a function when we need to use a piece of code multiple times.