Java Tutorials
Convert JSON to XML using Gson and JAXB: JAVA Example
What is JSON? JSON is an abbreviation for Javascript Object Notation, which is a form of data that...
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.
In this tutorial, you will learn-
Syntax:
function functionname()
{
lines of code to be executed
}Try this yourself:
<html>
<head>
<title>Functions!!!</title>
<script type="text/javascript">
function myFunction()
{
document.write("This is a simple function.<br />");
}
myFunction();
</script>
</head>
<body>
</body>
</html> You can create functions with arguments as well. Arguments should be specified within parenthesis
Syntax:
function functionname(arg1, arg2)
{
lines of code to be executed
}Try this yourself:
<html>
<head>
<script type="text/javascript">
var count = 0;
function countVowels(name)
{
for (var i=0;i<name.length;i++)
{
if(name[i] == "a" || name[i] == "e" || name[i] == "i" || name[i] == "o" || name[i] == "u")
count = count + 1;
}
document.write("Hello " + name + "!!! Your name has " + count + " vowels.");
}
var myName = prompt("Please enter your name");
countVowels(myName);
</script>
</head>
<body>
</body>
</html> You can also create JS functions that return values. Inside the function, you need to use the keyword return followed by the value to be returned.
Syntax:
function functionname(arg1, arg2)
{
lines of code to be executed
return val1;
}Try this yourself:
<html>
<head>
<script type="text/javascript">
function returnSum(first, second)
{
var sum = first + second;
return sum;
}
var firstNo = 78;
var secondNo = 22;
document.write(firstNo + " + " + secondNo + " = " + returnSum(firstNo,secondNo));
</script>
</head>
<body>
</body>
</html>
What is JSON? JSON is an abbreviation for Javascript Object Notation, which is a form of data that...
How to read a file in Java? Java provides several mechanisms to read from File. The most useful...
You can use JavaScript code in two ways. You can either include the JavaScript code internally within...
Here are Java Collections Interview Questions for fresher as well as experienced candidates to get...
What is Armstrong Number ? In an Armstrong Number, the sum of power of individual digits is equal...
This Java Development Kit(JDK) allows you to code and run Java programs. It's possible that you...