Course Banner


Javascript Functions

A function is a piece of code that can be called anywhere in the program. The function lies idle until it is referenced or called upon to do its work. Functions are time saver because same piece of code you do not have to write again and again.
There are mainly three types of functions
1. Function without parameters
2. Functions with parameters
3. Function with return value
1. Function without argument
The function which does not have any argument is called function without argument.
As you know the main purpose of the function is re-usability.
Let’s take an example: Suppose you have a website in which you want to display current year as with copy right which will be dynamically changing as the year passes by.
For that, we can create a function inside which we will write code to display current year.
Below is the code to display current year in JavaScript
:
<script>
function copyright(){
var currentTime = new Date();
year = currentTime.getFullYear();
document.write("Copyright&copy; " + year);
}
copyright();
</script>
The above code will generate output like this:
Copyright© 2018
Now, suppose you want to display this into all the pages in the footer of your website. You just need to include JavaScript file in all the files and call only function name. You don’t need to write code in all the pages.
That means, the function is reusable.
Also, the above function is example of function without argument.