Sunday 1 April 2018

Functions in javaScript

Function is a Self-contained block of statement.

Functions are used to divide a large code into module so that it is easy to debug the program.

 

Advantages:
Code Reusability.

Less Code: Less code is required in  javaScript because you don’t need to write the code again and again. It saves code


 

User defined Functions

The function which is defined by user is called User defined function.

 

Function definition

 

function  function_name(parameters)

{  

//function body

}  

Function Call

function_name();

 

Example 1( Using No parameter)

<script>

function show()

{ 

alert("hello welcome to  learningpoint92"); 

} 

</script> 

<input type="button" onclick="show()" value="getOutput "/>


Output:

Welcome To LearningPoint92

 

 

OR

<script> 

function show()  //function definition

{ 

document.write("hello welcome to  learningpoint92"); 

} 

show();  //function call

</script>

 

Output:

hello welcome to  learningpoint92

 


Example 2(Using parameter)

<script> 

function add(num1,num2){ 

alert("sum is:"+(num1+num2)); 

} 

</script> 

<input type="button" value="click" onclick="add(12,18)"/> 

 

Output:

Sum is:30

 

Example 3(Return Statement)

<script> 

function show(){ 

return "Hello surya";

}

 document.write(show()); 

</script> 

Output:

Hello surya

 

Example 4(Taking value from user)

<script> 

function add()  // defining function

{

var x =parseInt(prompt("Enter first number"));

var y =parseInt(prompt("Enter second number"));

alert("Sum is:"+(x+y));

}

add(); //function call

</script> 

Output:

Enter first number

23

Enter second number

20

Sum:43



1 comment:

  1. Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.

    ReplyDelete

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...