Function Parameters

We see what are the parameters in the functions. We see how to define functions that receive parameters in the Javascript language and how to call functions passing parameters.

In it we have previously talked about functions. Specifically, this is the third article that we address on the subject.

The ideas that we have previously explained about functions are not the only ones that we must learn to handle them in their full power. Functions also have input and output data. In this article we will see how we can send data to Javascript functions.

parameters

Parameters are used to pass values ​​to functions. A function will work with the parameters to perform the actions. To put it another way, parameters are the input values ​​that a function receives.

To give an easy-to-understand example, a function that performs a sum of two numbers would have those two numbers as parameters. The two numbers are the input, just as the output would be the result of the addition, but we’ll see that later.

Let’s see a previous example in which we created a function to display a welcome message on the web page, but now we are going to pass a parameter that will contain the name of the person to be greeted.

function writeWelcome(name){ document.write(“

Hello ” + name + “

“) }

As we can see in the example, to define a parameter in the function we have to put the name of the variable that will store the data that we pass to it. That variable, which in this case is called name, will have the value of the data that we pass to the function when we call it. Also, the variable where we receive the parameter will live during the execution of the function and will cease to exist when the function finishes its execution.

See also  Is the Alexa traffic ranking reliable?

To call a function that has parameters, place the value of the parameter in parentheses. To call the function in the example, you would write:

writeWelcome(“Alberto Garcia”)

When calling the function like this, the name parameter takes the value “Alberto García” and when writing the greeting on the screen it will write “Hello Alberto García” between

tags.

Parameters can receive any type of data, numeric, textual, Boolean, or an object. We don’t really specify the type of the parameter, so we must take special care when defining the actions we perform inside the function and passing values ​​to it, to make sure everything is consistent with the data types we expect our variables or parameters to have.

multiple parameters

A function can receive as many parameters as we want and to express it, the names of the parameters are separated by commas, inside the parentheses. Let’s quickly see the syntax for the function from before, but made so that it receives two parameters, the first the name to greet and the second the color of the text.

function writeWelcome(name,textColor){ document.write(““) document.write(“

Hello ” + name + “

“) document.write (““) }

We would call the function with this syntax. Between the parentheses we will place the values ​​of the parameters.

var myName = “Pepe” var myColor = “red” writeWelcome(myName,myColor)

I have placed between the parentheses two variables instead of two quoted texts. When we put variables between the parameters, what we are actually passing to the function are the values ​​that the variables contain and not the variables themselves.

Parameters are passed by value

Regarding the use of parameters in our Javascript programs, we have to know that function parameters are passed by value. This means that we are passing values ​​and not variables. In practice, even if we modify a parameter in a function, the original variable that we had passed will not change its value. It can be easily seen with an example.

function passByValue(myParameter){ myParameter = 32 document.write(“I have changed the value to 32”) } var myVariable = 5 passByValue(myVariable) document.write(“the value of the variable is: ” + myVariable)

In the example we have a function that receives a parameter and that modifies the value of the parameter assigning it the value 32. We also have a variable, which we initialize to 5 and later call the function passing this variable as a parameter. Since we modify the value of the parameter inside the function, it could happen that the original variable changed its value, but since the parameters do not modify the original value of the variables, it does not change its value.

In this way, once the function has been executed, when printing the value of myVariable on the screen, the number 5 will be printed, which is the original value of the variable, instead of 32, which was the value with which we had updated the parameter.

In Javascript you can only pass variables by value.

Now that we’ve learned how to send data to functions, via parameters, we can .

Loading Facebook Comments ...
Loading Disqus Comments ...