Declaring a Function
Creating a function, we give it a name and write the statements which performs the desired task inside the curly braces. This is called a FUNCTION DECLARATION.
We declare a function using the function keyword.
We give the function a name which is also called an identifier, followed by parentheses.
The statements that perform the task sit in a code block, which is inside the curly braces.
functions sayHello()
{
document.Write('Hello!');
}
This function is very basic, it contains only one statement and it shows how to write a function, most of the functions are likely to consist of many more statements.
The important thing to remember is that functions stores the code which is required to perform a specific task and can be called to perform the needed task whenever its called.
Whenever there are different parts of a script need to perform the same task, there we need not repeat the same statements multiple times, so we use a function to do it- as to reuse the same code.
Calling a Function
After declaring the function, we can execute all the statements between the curly braces with only one line of code. This process is called " calling the function ".
sayHello(); // IT IS A FUNCTION NAME
To run the code in the function, we use function name followed by parentheses.
In the above code we say that this code calls a function.
We can call the same function as many times as we want within the same JavaScript file.
funtion sayHello() // 1 { document.write('Hello'); // 3 } //Code before hello... sayHello(); // 2 // Code after hello... // 4
The function can store the instructions for a specific task.
eg. function sayHello()
When we need the script to perform that task, we call the function.
eg. sayHello();
The function executes the code in that code block.
eg. document.write('Hello');
When it has finished, the code continues to run from the point where it was initially called.
eg. // Code after hello...
From, JON DUCKETT's------JAVASCRIPT AND JQUERY, interactive front-end web development
Sometimes we will see a function called before it has been declared. This still works because the interpreter runs through a script before executing each statement, so it will know that a function declaration appears later in the script. But for the moment, we will declare the function before calling it.
Declaring Functions that need Information
Sometimes a function needs specific information to perform its task. In such cases, when you declare the function you give it parameters. Inside the function, the parameters act like variables.
If a function needs information to work, you indicate what it needs to know in parentheses after the function name.
The items that appear inside these parentheses are known as the parameters of the function. Inside the function those words act like variable names.
function getArea(width, height) //here width and height are called parameters
{
return width * height; // The parameters are used like variables within the function
}
This function will calculate and return the area of a rectangle. To do this, it needs the rectangle's width and height. Each time you call the function these values cloud be different.
This demonstrates how the code can perform a task without knowing the exact details in advance, as long as it has rules it can follow to achieve the task.
So, when we design a script, you need to note the information the function will require in order to perform its task.
If we look inside the function, the parameter names are used just as you would use variables. Here, the parameter names width and height represent the width and height of the wall.
Calling Functions that need Information
When we call a function that has parameters, we specify the values it should use in the parentheses that follow its name. The values are called arguments, and they can be provided as values or as variables.
ARGUMENTS AS VALUES
When the function below is called, the number 3 will be used for the width of the wall, and 5 will be used for its height.
getArea(3, 5);
ARGUMENTS AS VARIABLES
We do not have to specify actual values when calling a function-we can use variables in their place. So the following does the same thing.
wallWidth = 3; wallHeight = 5; getArea(wallWidth, wallHeight);
PARAMETERS VS ARGUMENTS
People often use the terms parameters and argument interchangeably, but there is a subtle difference.
Here we can see that the getArea() function is being called and the code specifies real numbers that will be used to perform the calculation or variables that hold real numbers.
When the function is declared, we can see the words width and height used. The curly braces of the function, those words act like variables. These names are the parameters.
These values that you pass into the code -the information it needs to calculate the size of this particular wall, are called arguments.