Functions
Functions
Functions
What is function?
• Functions are very important and useful in any programming language as they make the
code reusable.
• A function is a block of code which will be executed only if it is called.
• If you have a few lines of code that needs to be used several times, you can create a
function including the repeating lines of code and then call the function wherever you
want.
• Functions are the main “building blocks” of the program. They allow the code to be
called many times without repetition.
How to Create a Function in JavaScript
Use the keyword function followed by the name of the function.
After the function name, open and close parentheses.
After parenthesis, open and close curly braces.
Within curly braces, write your lines of code.
Function declaration
function functionname(){
}
Example
function myFunction()
{
document.write("This is a simple function.");
}
myFunction(); - function call
You can pass less or more arguments while calling a function. If you pass less arguments
then rest of the parameters will be undefined.
If you pass more arguments then additional arguments will be ignored.
Example
function ShowMessage(firstName, lastName) {
document.write("Hello " + firstName + " " + lastName);
}
ShowMessage("Steve", "Jobs", "Mr.");
ShowMessage("Bill");
ShowMessage();
Function with default values
function showMessage(from, text = "no text given") {
document.write( from + " " + text );
}
showMessage("Ann");
Now if the text parameter is not passed, it will get the value "no text given"
Here "no text given" is a string, but it can be a more complex expression, which is only
evaluated and assigned if the parameter is missing.
Global and Local variables
Variables hold the data or information which can be changed anytime. JavaScript use reserved
keyword var to declare variables. In JavaScript, there are two types of variable and also it tells you
where in your program you are allowed to use the variables and functions that you’ve defined.
Local Variable:
When you use JavaScript, local variables are variables that are defined within functions. They have
local scope, which means that they can only be used within the functions that define them.
Global Variable:
In contrast, global variables are variables that are defined outside of functions. These variables have
global scope, so they can be used by any function without passing them to the function as parameters.
Global and Local variables
Within the body of a function, a local variable takes precedence over a global variable with the same
name. If you declare a local variable or function parameter with the same name as a global variable,
you effectively hide the global variable.
document.write(result);
Return Value
There may be many occurrences of return in a single function. For instance:
function checkAge(age) {
return true;
} else {
return false;
if ( checkAge(age) ) {
} else {
}
Function Expression
JavaScript allows us to assign a function to a variable and then use that variable as a
function. It is called function expression.
var add = function sum(val1, val2) {
return val1 + val2;
};
var full;
full = first + “ “ + last;
return full;
}
function secondFunction() {
var result;
result = concatenate('Zara', 'Ali');
document.write (result );
};
secondFunction();
Examples
function greet(name)
{
return "Hello " + name + "!";
}
document.write(greet("Eric"));
Examples
Define a function called multiplyFive which accepts a number and returns that number
multiplied by 5.
function multiplyFive(number) {
return number * 5;
}
document.write(multiplyFive(3));
document.write(multiplyFive(5));
Examples
Examples