Loading...

JavaScript Functions


in this tutorials we are going to learn about what is a function in javascript, how to create a function, how to call or invoke a function, parameters or arguments, return statement in a function in javascript.


What is a function in javascript ?


a function is a block which consists of a group of statements to perform a specific task.
it encourages the modoularity and re-usablity of the code in the program.


How to create a function in javascript ?


it is defined by a function keyword with a function name and followed by opening and closing parentheses ( )

the general syntax is :
        		     					    		
// function without parameters

function funName(){
	// statements
}

// function with parameters

function funName(param1, param2){
	// statements
}
        		     					    		
        		     					    	
example:
        		     					    		
// function without parameters

function greetMessage(){
	document.write("hello user");
}

// function with parameters

function greetMessage(user_name){
	document.write("hello" + user_name)
}
        		     					    		
        		     					    	


How to call or invoke a function in javascript ?


calling or invoking a function is easy just use function name followed by parentheses with semi-colon.
for example :
        		     					    		
function greetMessage(){
	document.write("welcome to tutorialsinfo");
}

// call or invoke a greetMessage
greetMessage();

output:

welcome to tutorialsinfo
        		     					    		
        		     					    	


Parameters or Arguments in Function


Parameter
parameters are the variables used in a function definition.
for example :
        		     					    		
function greetMessage(user){
	document.write(" welcome to tutorialsinfo : " + user);
}

        		     					    		
        		     					    	
Argument
Arguments are the variable or value which is pass when calling a function.
when we pass argument in function their value is transfer to the parameter variable.
for example :
        		     					    		
function greetMessage(user){
	document.write(" welcome to tutorialsinfo : " + user);
}

// call or invoke a greetMessage

var x = "alex"

greetMessage(x);

output:

welcome to tutorialsinfo : alex
        		     					    		
        		     					    	
in the above example when the var x is passed in the greetMessage(x) function it represents argument here.
and the argument value is pass to the parameter var user.
then the function process the user value and print the message " welcome to tutorialsinfo : alex " as output.


Return statement in a function in javascript


return is a keyword in javascript.
it is used to return value to a caller ( where the function call happened).
when a function reach to its return statement the function stops the execution after executing the return statements.
it is best practice to use the return statement in the end of the block statements.
otherwise the the statements after return will never be executed.
for example :
        		     					    		
// pass arguments to a function

document.write(addNumber(11, 99));

// function to add two numbers

function addNumber(x, y) {
  z = x + y;
  return z;
  document.write("the result is : " + z);            
}

output:

110
        		     					    		
        		     					    	
in the above program the last statement of document.write("the result is : " + z); is not executes.
because of the return statement. it is proved that after return the interpreter stops the execution.


Assign Function to a variable in javascript


in javascript we can use a function as a value to a variable.
when we use function in this way the function must have to use return statement in it.
for example :
        		     					    		

var x = addNumber(21, 30);

document.write(x);

function addNumber(x, y) {
  return x + y;            
}

output:

51
        		     					    		
        		     					    	
in the above example we use a var x in which we assign a function addNumber().
addNumber(21, 30) function returns 51 and then assign it to it like this x = 51.