Page 200 - Web_Application_v2.0_C12_Fb
P. 200

Function Naming Rules
              JavaScript naming rules for functions are as follows:

                Function names can contain letters (a-z, A-Z), digits (0-9), underscores (_), and dollar signs ($).
                Function names must begin with a letter, an underscore (_), or a dollar sign ($) but cannot start with a digit.
                Function names are case-sensitive.

                JavaScript keywords cannot be used as function names.
                Function names cannot contain spaces.
              A list of the arguments for the function, enclosed in parentheses and separated by commas.


              Creating a Function
              Defining a function in JavaScript takes multiple important elements that determine the functionality of the
              function and how it may be utilized. Below is a detailed walk-through of how to define a function in JavaScript,
              step-by-step:
              Step 1:   The function Keyword is used to declare a function in JavaScript, we begin with the function keyword.
                        This informs JavaScript that we are about to declare a new function.
              Step 2:   We provide a name for the function. Function names may include letters, numbers, underscores (_),
                        and dollar signs ($). Function names, however, should not begin with a number.
              Step 3:   We can define parameters. Parameters are holders that serve as variables for values that will be
                        used when the function is invoked. A function may have zero or more parameters. The parameters
                        are utilized within the function body and are considered local variables.
              Step 4:   The function body is defined with curly braces {}. Here you need to insert the code that should be
                        processed upon calling the function.
              Step 5:   If you wish for the function to return a value when invoked, you can make use of the return keyword.
                        The return command stops the execution of the function and, if desired, sends a value back to the
                        location where the function was invoked.
              The syntax of a function is as follows:

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

                  }
              For example:

                  function greet(name) {
                  document.write("Hello, " + name + "!");
                  }
              In this example:
                 greet is the function name.
                 name is a parameter, which is used inside the function to greet a person.
                 The function uses document.write to output a message.

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

                  hello("Alice");

                198   Touchpad Web Applications (Ver. 2.0)-XII
   195   196   197   198   199   200   201   202   203   204   205