Functions are blocks of code that you can execute by simply entering the function's name. Here's their syntax:
function function_name(parameters)
{
Whatever the function does
return statement (not always necessary)
}
To declare a function, you simply have to put the word function followed by the function's name, which can have any characters from a-z to 0-9, and "_". No special characters. Parameters are optional; I'll get to them later. Inside the brackets of the function go the code that the function will execute when called. And finally, the return statement. You only need it if your function is going to give any information back to the code that called it.
So, you've seen the template for declaring a function. It's a lot of information, so I'm just going to make a simple little function to get you started.
function getName()
{
var name;
name = prompt("What's your name?","");
return name;
}
This function simply makes a popup window that asks for the user's name, and returns it when called. That prompt thing is also a function built into the language. It just makes a popup window that takes in information. I'll talk about it later too.
Now onto a function with parameters. Parameters are variables that the function takes in, and these variables only exist during the execution of the code within the function. You can take in as many parameters as is necessary.
function Add_Two_Numbers_Together(first_number, second_number)
{
var result = first_number + second_number;
return result;
}
A stupidly simple function, but it conveys the abilities of parameters. This function takes in two numbers, adds them together, and returns the result. So now that you know how to MAKE functions, let's learn how to USE functions. You call a function by simply calling it by name, being sure to include any parameters in the parentheses.
var name = getName(); var first = 5, second = 12; var result = Add_Two_Numbers_Together(first,second); var my_result = Add_Two_Numbers_Together(4,12);
Notice how I set a variable equal to the function. This only works if the function returns something. If it doesn't, the function should simply stand alone. Also notice that the variables passed in the second function don't have the same name as they do in the function's declaration. You may have guessed that they don't have to be. They just need to be in the proper order - the value of the variable in the CALL is passed on to the variable in the function's DECLARATION.
This leads me to another important aspect of variables. You can make a variable global or local. Global variables exist on top of everything, so to speak. They can be directly accessed by any function - they don't have to be passed or antyhing. To make a variable global, just declare it outside of a function. To make it local, just declare it within a function. When this is done, it'll only exist within that function, and any functions that it passes that variable to.
I think it's high time that you actually learned how to incorporate these concepts into your web page. And that you shall learn in the next lesson!