[go: up one dir, main page]

0% found this document useful (0 votes)
9 views4 pages

Functions

JavaScript functions are sets of statements designed to perform specific tasks, allowing for code reusability and reducing repetition. Functions can be user-defined or built-in, and they can take inputs and return outputs. Proper naming conventions must be followed, and functions can also be treated as objects in JavaScript.

Uploaded by

justthat710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Functions

JavaScript functions are sets of statements designed to perform specific tasks, allowing for code reusability and reducing repetition. Functions can be user-defined or built-in, and they can take inputs and return outputs. Proper naming conventions must be followed, and functions can also be treated as objects in JavaScript.

Uploaded by

justthat710
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Functions

JavaScript Functions, just like in any other language, is a set of statements that are used to
perform a specific task, like adding two numbers, finding the square of a number, or any
user-defined task.

JavaScript Function: Definition


JavaScript function is a set of statements that are used to perform a specific task. It can take
one or more input and can return output as well. Both, taking input and returning an output
are optional.
Using functions avoids repetition of the code as we need to write the code only once and
then the code can be called anywhere using function name.

A function can be helpful in the following scenarios:

● For instance, suppose you want to add some numbers and display the results on a web
page. In that case, you can define the code for adding the numbers in a function and call
the function whenever needed.

● For repetitive tasks like displaying a message whenever a web page is loaded into the
browser, we can use functions.

Following image shows the structure of a JavaScript function:

Advantage of JavaScript function


Functions are useful in organizing the different parts of a script into the several tasks that
must be completed. There are mainly two advantages of JavaScript functions:

1. Code reusability: We can call a function several times in a script to perform their tasks so
it saves coding.

2. Less coding: It makes our program compact. We don't need to write many lines of code
each time to perform a common task.

Rules for naming functions:

● It must be case sensitive.


● It must start with alphabetical character (A-Z) or an underscore symbol.
● It cannot contain spaces.
● It cannot be used as reserve words.
JavaScript Function Example
I.
<script>
function msg(){
alert("hello! this is message");
}
</script>
<input type=button onclick=msg() value="call function"/>

II. JavaScript Function Arguments


<script>
function getcube(number){
alert(number*number*number);
}
</script>
<form>
<input type=button value=click onclick=getcube(4) />
</form>

Broadly, we can categorize functions into two category:


1. User defined Function
2. Built-in Function

JavaScript User-defined Function


JavaScript allows us to define our own functions as per our requirement.

● Creating a User-defined Function:

In JavaScript, function is defined by using the function keyword followed by the function
name and parentheses ( ), to hold params(inputs) if any. A function can have zero or more
parameters separated by commas.The function body is enclosed within curly braces just
after the function declaration
part(function name and params), and at the end of the function body, we can have
a return statement to return some output, if we want.

● Following is the syntax for JavaScript user-defined functions

function function_name(parameter-1, parameter-2,...parameter-n)


{
// function body
}

where, function_name represents the name of the function and parameter-1, ... ,
parameter-n represents list of parameters.
● Calling a User-defined Function:
After creating a function, we can call it anywhere in our script. The syntax for calling a
function is given below:
function_name(val-1, val-2, ..., val-n);
Here, list of parameters represents the values passed to the funtion during function call.

● User-defined Function return Statement:


A function may or may not have a return statement, because not all functions return an
output.
A return statement is used to specify the value/result/output that is returned from a function.

● Following is the syntax for using the return statement:

return value;
The return keyword returns the value to the calling statement.

● JavaScript User-defined Function Example:


<html>
<head>
<script>
function mySumFunction(a, b)
{
return a+b;
}
let x = myFunction(8, 7);
alert(x);
</script> </head></html>

JavaScript Built-In Functions


Functions that are provided by JavaScript itself as part of the scripting language, are known
as built-in functions. JavaScript provides a rich set of the library that has a lot of built-in
functions. Some examples of the built-in functions are : alert(), prompt(), parseInt(), eval()
etc.
JavaScript Function as Objects
Functions in JavaScript also are treated as objects and can be assigned to variables. Once
we assign a variable with a function, we can then use the variable name to call the function.

● Let's take an example for this:


let x = function someFunction(y) {
document.write("Function called with value: " + y);
}
// we can call the function using the variable now
x(10);
Function called with value: 10

You might also like