Technology Programming

JavaScript Functions As a Function Argument

    Functions and Arguments

    • In computer programming, a function defines a block of code that performs a specific task. A programmer can then call the code by its name in order to perform that task. In JavaScript, for example, a programmer might define a function like this example:

      function doubleNum(x)

      {

      return x*2;

      }

      The "x" in parentheses is what is called an "argument," or a variable that a programmer supplies to the function in order for it to work. Then, when a programmer wishes to use the double function, he would call it by name, as in this example:

      var y = double(3); //y = 6

    Functions and Object Oriented Programming

    • Object Oriented Programming (OOP) defines a paradigm in which programmers represent data as "objects," or discrete entities that store data and functions. The advantages of OOP are many, but a relevant one here is the fact that JavaScript, as an object oriented programming language, stores functions as objects. When a programmer defines a function, JavaScript saves it as an object in memory. Essentially, this means that a function defined by a programmer can also be assigned to a variable, the same way as a sentence, an integer, or a Boolean value.

    Examples of Assigning Functions to Variables

    • When a programmer defines a function, she can immediately assign the function to a variable. The variable will then behave as a function, taking arguments and returning values (depending on how the programmer defines the function). The following code is an example of assigning a function to a variable in JavaScript:

      var double = function doubleNum(x)

      {

      return 2 * x;

      }

      In this example, the variable "double" takes the code for the function. The function is the same as the "doubleNum" function.

    Using the Function as an Argument

    • Programmers can pass functions as arguments to other functions in the same way he passes other variables. For example, this code passes the "doubleNum" function to the "quadNum" function, which multiplies a number by two, and then by two again:

      function quadNum(x, doublefunction)

      {

      var double = doublefunction();

      return 2 * double(x);

      }

      quadNum(2, doubleNum);

      "quadNum" takes two arguments: the number x to multiply, and a function "doublefunction." Then, quadNum assigns doublefunction to the variable "double," and multiplies the result of double(x) and 2. To call quadNum, the programmer supplies a number and the doubleNum function.

Related posts "Technology : Programming"

Brochure Design And Its Budget

Programming

Website designing delhi-web design services India-Website Development Company India

Programming

The Secrets and techniques Rob Fore Won't Tell you!

Programming

Where Do You Get Podcasting Ideas?

Programming

Avoid Hacking With The Help of WordPress Development Company

Programming

Sirius Radio - All You At Any Time Wished to Know

Programming

How to Use a Check Box to Filter a List

Programming

Get professional help from website development Dublin- promote business growth

Programming

PHP Shopping Carts

Programming

Leave a Comment