You can write functions to define specific tasks that may be used at many points in a script. These functions are referred to as programmer-defined functions. The actual state-ments defining the function are written only once and are hidden from other functions.
A function is invoked (i.e., made to perform its designated task) by a function call. The function call specifies the function name and provides information (as arguments) that the called function needs to perform its task. A common analogy for this structure is the hierarchical form of management. A boss (the calling function, or caller) asks a worker (the called function) to perform a task and return (i.e., report back) the results when the task is done. The boss function does not know how the worker function performs its des-ignated tasks. The worker may call other worker functionsâ€”the boss will be unaware of this. Weâ€™ll soon see how this â€śhidingâ€ť of implementation details promotes good software engineering. Figure 9.1 shows the boss function communicating with several worker functions in a hierarchical manner. Note that worker1 acts as a â€śbossâ€ť function to worker and worker5, and worker4 and worker5 report back to worker1.
Functions are invoked by writing the name of the function, followed by a left paren-thesis, followed by a comma-separated list of zero or more arguments, followed by a right parenthesis. For example, a programmer desiring to convert a string stored in variable inputValue to a floating-point number and add it to variable total might write
total += parseFloat( inputValue );
Methods are called in the same way, but require the name of the object to which the method belongs and a dot preceding the method name. For example, weâ€™ve already seen the syntax document.writeln("Hi there.");. This statement calls the document objectâ€™s writeln method to output the text.
Copyright Â© 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.