Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 113 - CodePilot V5.0 C8
P. 113

    FUNCTIONS IN JAVASCRIPT

                     A block of code created specifically to carry out a specific task is referred to as a JavaScript
                     function. When a JavaScript function is called, it is executed.

                     CREATING AND CALLING A FUNCTION
                     Defining a function in JavaScript involves several key elements that determine the functionality of
                     the function and how it may be utilised.

                     The syntax of creating a function is as follows:

                     function name (parameter1, parameter2, parameter3,...,parameterN)
                     {
                     // code to be executed
                     }

                     To execute the function and pass an argument to the parameter, we invoke it by its name and
                     pass an argument (value).

                     The syntax of calling a function is as follows:

                     function name (argument1, argument2, ...); //invoking the function
                     For example,

                     function hello (name)
                     {
                                                                                                        QR QUEST
                     document.write("Hello, " + name + "!");
                                                                                                     Visit the given link
                     }                                                                       to learn about JavaScript

                     hello("JavaScript");                                                            function:
                     The output of the preceding program is as follows:                   https://www.youtube.com/watch?v=H-
                                                                                                    FaxylC7bUc
                     Hello, JavaScript!                                                  Answer the given questions:
                     In  this  example,  hello is the function  name, name is a          1.  What are some benefits of using
                     parameter, which is used inside the function to greet a person.       functions in programming?
                     The function uses document.write() to output a message.             2. How is a function defined?

                     To execute  the function  and pass  the argument value
                     “JavaScript”, we call the function as hello(“JavaScript”);.

                        Code
                          5       Create a web page to demonstrate the use of functions in JavaScript.

                     <!DOCTYPE HTML>

                     <HTML>
                     <HEAD>

                                                                                                                      111
                                                                                                JavaScript for Beginners

| 108 109 110 111 112 113 114 115 116 117 118 |