Having extracted the code that you want to be able to run multiple times and wrapped it inside a function the next step is to replace all of the occurrences of the original code with calls to the function. A function call works slightly differently from the way you define variables and assign values to variables because with functions we can not only define and assign, we can also run functions.
As well as passing parameters to a function we can also return a value from a function.Where a function returns a value of a given type we can substitute a call to the function anywhere in our code where we want to reference a variable of that type. When the statement runs, the function will be called and the value that is returned will be substituted into the original statement at the point where the function call appears.
You don't necessarily have to write all your functions yourself as there are a number of function libraries available that others have written to perform a large number of commonly needed tasks which the authors have made available for people to use. If you incorporate one or more such libraries into your page then you can call the functions in those libraries the same way that you can call built in functions and those functions that you have written for yourself.