Function
JavaScript function
A JavaScript function is a block of reusable code designed to perform a specific
task. Functions are essential for organizing code, making it more readable, and allowing for
code reuse.
There are two types of functions in JavaScript
Built-in Functions: The built-in functions are already defined in JavaScript.
Some built-in functions are: prompt( ) , parseInt( ) , parseFloat( ) , isNan( )
etc.
User Defined Functions in JavaScript The functions created by the user to
perform a specific task are called user defined functions.
Function Syntax/ Creating a Function
A JavaScript function is defined with the function keyword, followed by a name,
followed by parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules
as variables).
The parentheses may include parameter names separated by commas:
(parameter1, parameter2, ...)
The code to be executed, by the function, is placed inside curly brackets: {}
Example:
function name(parameter1, parameter2, parameter3)
{
// code to be executed
}
Example: Below is a function called showUser that shows a user's name after getting a
String parameter containing the user's name.
Ans:
<html>
<body><script>
function showUser(username)
{
[Link]("User: " + username);
}
showUser("Rahul Kumar");
</script></body>
</html>
Invoking the Function/ Calling the Function
The code within the function runs when it is called or invoked after it has been
defined. When a user-defined function is invoked, program control is passed to the
called function. To call a function use the name of the function along with the ( ).
Example:
Using the keyword return in a function:
When JavaScript encounters a return statement( which is the keyword return followed by a
value or a variable), the function will terminate and return the value to the "caller" as the
return value.
The return value may be stored in a variable when the function is called.
xyz = func_name(arg1,arg2,arg3)
The return value may be output by calling the function within the output statement.
[Link](func_name(arg1,arg2,arg3))
Example:
<script>
function sum(x,y)
{
var z= x+y
return z
}
sum(4,5)
</script>
Invoking or calling a function multiple times
A function can be called multiple times within a program and each time we can pass
different arguments to it.
Example: Write a JavaScript program used to create a function with two parameters i.e.
product(x,y) to calculate product of two numbers. Call the created function multiple times
with new values.
Ans:
<html>
<body>
<script>
function product(x,y)
{
var z= x*y
[Link](“Product of two numbers are=”+z)
}
product(4,5)
product (2.1,8.3)
product (6,9.1)
</script>
</body>
</html>
PRACTICE QUESTIONS
1: Which of the following statements is not true for function arguments/parameters?
a. Function parameters are listed between parenthesis ( ).
b. The number of function parameters is fixed.
c. Function arguments are the values that the function receives when it is called.
d. The parameters act as local variables within the function.
Ans: The number of function parameters is fixed
2: In JavaScript, the definition of a function starts ____
a. with the return type, function keyword, function name and parentheses.
b. with the function keyword, function name and parentheses.
c. with the return type, function name and parenthesis.
d. with the function name, parenthesis and return type.
Ans: with the function keyword, function name and parentheses.
3: List the Built-in function for each of the Following:
1. A function to take user input as string. Ans: prompt()
2. The function used to convert to an integer. Ans: parseInt()
3. A function to convert to a floating-point number. Ans: parseFloat()
4. A function to check if something is Not a Number. Ans: isNaN()
5. Used to display data or a message in a box on the window. Ans: alert()
4: What will be the output of the following codes:
Ans:
5: What will be the output of the following codes:
Ans:
6. What will be the output of the following codes:
Output
The number is not prime
The number is prime
7. What is the syntax or logical Error in the following codes? Rewrite the code with the correct
syntax and underline the errors :
a) A function to find whether the number is odd or even.
Ans:
function chknum(a)
{
if(n%2==0)
{ [Link]("The number is even”+”<br>”)}
else
{ [Link]("The number is odd”+”<br>”)
}}
8. A function to find the sum of 3 numbers
Ans:
function sumfunc(a,b,c)
{
return a+b+c;
}
var sum= sumfunc(6,4,3)
[Link](“demo1”).innerHTML=sum
9. A function to find the value of a number raised to power another number using the Math
object method.
Ans:
<script>
function powerFunction(p1,p2)
{
return [Link](p1,p2);
}
[Link](powerFunction(4,3))
</script>