Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

JavaScript 009 – Writing A Function

1. JavaScript Function – Introduction

Like many programming languages, JavaScript provides functions for code re-usability. Even though JavaScript is not strongly typed, the function receives and sends parameters in type less fashion. In this JavaScript example, we will learn how to create function and call them passing arguments and use the return values.

2. Simple JavaScript Function

Have a look at the simple function below which prints a sample line of text in the document.

Simple Function Call
Simple Function Call

1) The function keyword, which marks the next coming piece of code as a JavaScript function definition.
2) It denotes the function name and argument list. The empty parenthesis denotes that the function does not take any parameters. In our case, the function name is SimplePrint.
3) This denotes the body of the function. A function body can have one or N number of JavaScript statements. A pair of Curley Braces marks the start and end of the function body. Inside the function body, duty of the function takes place, and, in our case, it is simply printing the line of text to the browser window. Note, we placed the function definition inside the <head> tag of the html so that the function will be available all over the html document. Means, one can use this function anywhere in the html’s body.
4) In the <body> tag, we call the function. This will print a line of text in the html browser window.
5) Shows the output of the function call in the browser window.

3. JavaScript Functions with Parameters

A Function can also accept parameters and based on the passed-in values the duty of the function will be decided. The below JavaScript function adds two numbers and prints the result. The numbers will come in as a parameter.

Below is the code:

JavaScript Function with Parameters
JavaScript Function with Parameters

1) The function here accepts two parameters. Note, there is no type specified here and hence the parameter can be a string or a number.
2) The body of the function adds the two numbers N1 and N2 and prints the result in the browser’s window via document.writeln function.
3) This is the calling code. Here, we call the function AddNumbers.
4) We pass two numbers 10, 4 as arguments. The arguments are comma separated list enclosed within the parenthesis.
5) Shows the result of calling function with parameters.

4. JavaScript Function with Return Value

A function can also return a value to the caller. To return a value to the calling code, one should use the returnstatement. Below is the example:

JavaScript Function with Return Value
JavaScript Function with Return Value

1) Shows a function which takes two numbers as parameters and adds them. The added result is in the variable N3.
2) Here the return statement is returning the value in the variable N3. Means, it returns the addition of the two input parameters.
3) The calling code is making call to the function SumGet by passing the arguments 3, 3. The variable N1 holds the returned result.
4) We make a call to the AddNumbers, which was written in the previous section. First param is the variable N1.
5) We call SumGet as a second parameter and the SumGet return value is passed as a second parameter to the AddNumbers function.

Executing the code will print the result as 16.

5. Code Reference

Categories: JavaScript

Tags: , ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.