Friday, 3 July 2020

Javascript functions | how to call javascript function - Online Help

Hello reader, today from this post we are going to learn about what is JavaScript functions? and how to call a function in javascript so read the article till the end and yeah let's gets started.

Javascript functions | how to call javascript function - Online Help


JavaScript functions:

Functions are a block of code that gets executed when called. Every function has a name, this name is used to call the function, which in turn result in the execution of the associate block of code. It takes zero or more parameters.

Functions can be defined both in the <head> and in <body> section of a document. However, to assure that a function is read or loaded by the browser before it’s called, it could be wise to put a function in the <head> section.


Defining functions:

A JavaScript functions definition consists of a function keyword, followed by the name of the function. A list of arguments to the function is enclosed in parentheses and separated by a comma. The statement within the function is enclosed in curly brace {}. The general format to define a function is,

Function functionname (parameter1, parameter2, …………..)
{
Statements;
}

Whereas,
  • Functionname is the name of the function.
  • parameter1, parameter2, ………….. are arguments or parameters to the function.


How to call functions in javascript:

A function is not executed before it is called. You can call functions containing arguments. The general form is:

functionname (parameter1, parameter2, …………..)


Return statement:

The return statement can be used to return any valid expression that evaluates to a sign value. A function that will return a result must use the return statement. This statement specifies the value which will be returned to where the function was called from.
A javascript function can have an optional return statement. This is required if you want to return a value from a function. The general format is,

return(); 

return(parameter);


Example:

<html>
<body>
<script type=”text/JavaScript”>
function funexample(a, b)
{
return a+b;
}
document.write (funexample(10,20));
</script>
</body>
</html>


Explanation:

The function is being called from another javascript method document.write. funexample() is a function passing two arguments 10 and 20.

Where 10 is being passed into the function funexample() as ‘a ‘ and 20 is being passed into the function as ‘b’.  

Once they are both passed in it return the sum of the two-argument. “return” simply means that we will return something back to the method or function that called our function funexample(). 

As a result, you can return sent back 30. The final statement that executed was more like document.write(30).


Example 2:

<html>
<head>
<title>example2</title>
</head>
<body>
<h2>
<script type=”text/JavaScript”>
functionsum(a, b)
{
c= a+b;
return c;
}
d= sum(12,8);
document.write (“the returned value is :” + d);
</script>
</h2>
</body>
</html>

The output of the above program is:

the returned value is: 20


Recursive function:

Recursion refers to the situation, wherein function call themselves. In other words, there is a call to specific functions from within the same function. It is called a recursive function.

Example:

<html>
<head>
<title>recursive function</title>
</head>
<body>
<script type= “text/JavaScript”>
var num=5;
function fact(num)
{
if (num>1)
{
document.write(“fact=”+num*fact(num-1));
}
else
{
document.write(“fact=”+num);
}
}
</script>
</body>
</html>

The result of the above program is,

Fact=120

I hope you guys understand what is javascript functions and how to define and call javascript functions. If you have any queries or doubt or you know more information about this article then please comment down below. Thank you for reading and visiting my blog.
.



No comments:

Post a Comment