DEV Community

Cover image for Arguments and Parameters,Generator Functions
Sarmin Akter Dipty
Sarmin Akter Dipty

Posted on

Arguments and Parameters,Generator Functions

Image description

Arguments and Parameters
In the following code block, you will create a function that returns the cube of a given number, defined as x:

The x variable in this example is a parameter—a named variable passed into a function. A parameter must always be contained in a variable and must never have a direct value.
Now take a look at this next code block, which calls the cube function you just created:

// Invoke cube function cube(10)
This will give the following output:
1000
In this case, 10 is an argument—a value passed to a function when it is invoked. Often the value will be contained in a variable as well, such as in this next example:

// A
ssign a number to a variable const number = 10 // Invoke cube function cube(number)
This will yield the same result:
1000
If you do not pass an argument to a function that expects one, the function will implicitly use undefined as the value:
// Invoke the cube function without passing an argument cube()
This will return:
NaN

Generator Functions

A generator function is a function that returns a Generator object, and is defined by the function keyword followed by an asterisk (*), as shown in the following:

// Generator function declaration function* generatorFunction() {}

Occasionally, you will see the asterisk next to the function name, as opposed to the function keyword, such as function generatorFunction(). This works the same, but function is a more widely accepted syntax.

Generator functions can also be defined in an expression, like regular functions:
// Generator function expression const generatorFunction = function* () {}

Top comments (0)