Java Script


JavaScript Tutorial


Admission Enquiry Form


Function with no argument and with return in JavaScript

When function does not take any value as an argument but return a value.


Syntax Function with no arguments and with return value

function functionName()
{  
 //statements to be executed  
 //return statement;
}  

Example of Function with no arguments and with return inside body tag:

<html>
<head>
<title>Function with no argument and with return</title>
</head>
<body>
<script>
function square()
{
var n=2,res;
res=n*n;
return (res);
}
var ans;
ans=square();
document.write("Square is "+ans);
</script>
</body>
</html>

Output:

Square is 4

Example of Function with no arguments and with return on button click:

<html>
<head>
<title>Function with no argument and with return</title>
<script>
function wish() //defintion of function
{
var x="Welcome to Compuhelp";
return (x);
}
</script>
</head>
<body>
<h2>Click on Button to see the output</h2>
<input type="button" name="btn1" value="click" onClick="var res=wish(); alert(res);">
</body>
</html>

Output:

In the above example,when we click on the button it will give the output as 'Welcome to Compuhelp'.