KEMBAR78
Java script functions | PPTX
JavaScript - Functions
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
JavaScript - Functions
 A function is a group of reusable code which can be
called anywhere in your program.
 This eliminates the need of writing the same code
again and again.
 It helps programmers in writing modular codes.
Functions allow a programmer to divide a big program
into a number of small and manageable functions.
Cont…
 The most common way to define a function in
JavaScript is by using the function keyword, followed
by a unique function name, a list of parameters (that
might be empty), and a statement block surrounded
by curly braces.
Syntax
 Syntax
 The basic syntax is shown here.
 <script type = "text/javascript"> <!-- function
functionname(parameter-list) { statements } //-->
</script>
Example
 It defines a function called sayHello that takes no
parameters −
 <script type = "text/javascript"> <!-- function
sayHello() { alert("Hello there"); } //--> </script>
Calling a Function
<html>
<head>
<script type = "text/javascript">
function sayHello() {
document.write ("Hello there!"); }
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello()" value = "Say Hello"> </form>
<p>Use different text in write method and then try...</p>
</body>
</html>
OUTPUT: Hello there!
Function Parameters
 <html>
 <head>
 <script type = "text/javascript">
 function sayHello(name, age) {
 document.write (name + " is " + age + " years old."); } </script>
 </head>
 <body>
 <p>Click the following button to call the function</p>
 <form>
 <input type = "button" onclick = "sayHello('Zara', 7)" value = "Say Hello">
 </form>
 <p>Use different parameters inside the function and then try...</p> </body>
 </html>
 Output: Zara is 7 years old.
Assignment Question
 Explain the Function in javascript with discuss the
following terms.
 Calling Function
 Function Parameter

Java script functions

  • 1.
    JavaScript - Functions Prof.Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    JavaScript - Functions A function is a group of reusable code which can be called anywhere in your program.  This eliminates the need of writing the same code again and again.  It helps programmers in writing modular codes. Functions allow a programmer to divide a big program into a number of small and manageable functions.
  • 3.
    Cont…  The mostcommon way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a list of parameters (that might be empty), and a statement block surrounded by curly braces.
  • 4.
    Syntax  Syntax  Thebasic syntax is shown here.  <script type = "text/javascript"> <!-- function functionname(parameter-list) { statements } //--> </script>
  • 5.
    Example  It definesa function called sayHello that takes no parameters −  <script type = "text/javascript"> <!-- function sayHello() { alert("Hello there"); } //--> </script>
  • 6.
    Calling a Function <html> <head> <scripttype = "text/javascript"> function sayHello() { document.write ("Hello there!"); } </script> </head> <body> <p>Click the following button to call the function</p> <form> <input type = "button" onclick = "sayHello()" value = "Say Hello"> </form> <p>Use different text in write method and then try...</p> </body> </html> OUTPUT: Hello there!
  • 7.
    Function Parameters  <html> <head>  <script type = "text/javascript">  function sayHello(name, age) {  document.write (name + " is " + age + " years old."); } </script>  </head>  <body>  <p>Click the following button to call the function</p>  <form>  <input type = "button" onclick = "sayHello('Zara', 7)" value = "Say Hello">  </form>  <p>Use different parameters inside the function and then try...</p> </body>  </html>  Output: Zara is 7 years old.
  • 8.
    Assignment Question  Explainthe Function in javascript with discuss the following terms.  Calling Function  Function Parameter