A function is a block of code wrapped in curly braces, preceded by a keyword function:
function functionname () { // Here is the code to execute }
When the function is called, the code inside the function is executed.
A function can be called directly when an event occurs (such as when a user taps a button) and can be called from anywhere by JavaScript.
Tip: JavaScript is case sensitive. The keyword function must be lowercase and must be called with the same casing as the function name.
When you call a function, you can pass a value to it, which is called a parameter.
These parameters can be used in functions.
You can send any number of arguments separated by commas (,):
MyFunction (Argument1,argument2)
When you declare a function, declare the argument as a variable:
function myFunction (var1,var2) { // Here is the code to execute }
Variables and parameters must appear in a consistent order. The first variable is the given value of the first parameter passed, and so on.
Sometimes, we want the function to return the value to the place where it was called.
This can be achieved by using the return statement.
When you use the return statement, the function stops execution and returns the specified value.
function myFunction () { var x=5; return x;}
The above function returns the value 5.
Note: The entire JavaScript does not stop executing, just the function. JavaScript will continue to execute code from where the function is called.
JavaScript Learning Notes (7)--javascript syntax function