Having real trouble understanding functions, some help would be great

Hi all,

I totally understand that in order to learn javascript I need to know how functions work, I understand the basics of passing in parameters and then calling the function with the values to maybe add something together etc I ve read countless articles about functions as well as books etc but I just dont get how they are used and when they should be used etc, the more advanced functions that have maybe 4 parameters and are doing different calculations and returning various values that get fired back into the script just totally confuses me.

What I would like to know is first of all how can I overcome this confusion and also any words of wisdom you may have. I will also add that I have no prior programming experience and have spent the last 2 months frequently hitting my head off a brick wall as I just cant understand javascript.


There are many reasons for using functions.

The simplest reason is that if you do not use functions then you could end up with a very large block of code that is difficult to understand. Taking parts of the code out into a function can make the large block of code a lot smaller and thus easier to understand.

You also use a function if a piece of code occurs many times in your program/script. Put the code into a function and call the function rather than replicate the code. The use of function parameters/arguments allows the same code to work on different data.

The most common use of functions in javascript is as event handlers. You can say, for example, when a user clicks a particular part of the page I want this function to be executed. There are lots of other events in addition to click for example getting or losting focus, mouse or key pressing, submitting forms.