A function is a chunk of code which you can use more than once in a program.  In JavaScript you did this:

function fred(theMessage) {                

Any time you want to pop up a message you can now use this function.  The message is passed as a parameter. 

Your functions and built-in functions

There was actually no point in creating this function because the same end result could be achieved by using the alert() function.  This is a function which is included in the JavaScript interpreter inside your browser.  There are many more and you have used some in PHP as well (e.g. print_r() and mysqli_fetch_array()).

You can also use functions written by other people.  People who have written code will sometimes make it available for use by other programmers.  You can include these and use them in your code to save you having to write them.  In JavaScript the best known library would be jquery.  In Windows libraries are known as DLLs.

Other names for functions

In JavaScript and PHP you have used functions.  In other languages there are things which are the same or similar with different names.  Procedures and modules are also blocks of code which can be re-used.  You may also see reference to routines and sub-routines which may be a bit different but are still blocks of code.