Return values ​​in Javascript functions

Functions can return values, through the return statement. We also see a note about the scope of variables in functions in Javascript.

We are learning about the use of functions in Javascript and by now we may have already realized how important they are to make more or less advanced programs. In this article we will continue learning about functions and specifically that you can also return values ​​with them. In addition, we will see some interesting use cases about functions that can clarify the scope of local and global variables a bit.

Returning values ​​in functions

Javascript functions can also return values. In fact, this is one of the most essential utilities of the functions, which we must know, not only in Javascript but in general in any programming language. So when a function is called, it can perform actions and provide a value as output.

For example, a function that calculates the square of a number will have that number as its input and the value resulting from finding the square of that number as its output. We saw data entry in functions previously in the article about . Now we have to learn about the output.

Let’s look at an example of a function that calculates the mean of two numbers. The function will receive the two numbers and return the mean value.

function average(value1,value2){ var result result = (value1 + value2) / 2 return result }

To specify the value that the function will return, use the word return followed by the value that you want to return. In this case, the content of the result variable is returned, which contains the calculated mean of the two numbers.

See also  /faq/491.php

Perhaps we are now wondering how to receive data returned by a function. Actually in the source code of our programs we can call the functions in the place that we want. When a function returns a value, the call to the function is simply replaced by that returned value. So, in order to store a return value of a function, we have to assign the call to that function as contained in a variable, and we would do that with the assignment operator =.

To illustrate this you can see this example, which will call the function media() and save the result of the media in a variable and then print it on the page.

var myMedia myMedia = media(12,8) document.write (myMedia)

multiple returns

In reality, in Javascript, functions can only return one value, so in principle we cannot make functions that return two different data.

Note: in practice, nothing prevents us from having a function return more than one value, but since we can only return one thing, we would have to put all the values ​​we want to return in a data structure, such as a . However, that would be a more or less advanced use that we are not going to see at this time.

Now, although we can only return one data, in the same function we can place more than one return. As we said, we will only be able to return one thing, but depending on what has happened in the function, it may be of one type or another, with some data or another.

See also  Guide for SSH access and basic commands with Linux servers

In this function we can see an example of using multiple returns. It is a function that returns 0 if the received parameter was even and the value of the parameter if it was odd.

function multipleReturn(number){ var remainder = number % 2 if (remainder == 0) return 0 else return number }

To find out if a number is even, we find the remainder of the division by dividing it by 2. If the remainder is zero, it is even and we return 0, otherwise -the number is odd- we return the received parameter.

Scope of variables in functions

Within functions we can declare variables. Regarding this matter, we must know that all the variables declared in a function are local to that function, that is, they will only be valid during the execution of the function.

Note: Even if we think about it, we can realize that the parameters are like variables that are declared in the function header and that are initialized when the function is called. Parameters are also local to the function and will be valid only when the function is executing.

It could be the case that we can declare variables in functions that have the same name as a global variable to the page. So, inside the function, the variable that will be valid is the local variable and outside the function the global variable to the page will be valid.

On the other hand, if we do not declare the variables in the functions, javascript will understand that we are referring to a global variable to the page, so that if the variable is not created, it is created, but always global to the page instead of local to the function.

See also  What is Vagrant, how to work with Vagrant

Let’s see the following code.

function global_and_local_variables(){ var variableLocal = 23 variableGlobal = “qwerty” }

In this case variableLocal is a variable that has been declared in the function, so it will be local to the function and will only be valid during its execution. On the other hand variableGlobal has not been declared (because before using it the word var has not been used to declare it). In this case, the variableGlobal variable is global to the entire page and will continue to exist even if the function ends its execution. Furthermore, if the variable variableGlobal existed before calling the function, as a result of the execution of this function, a hypothetical value of that variable would be pounded and replaced by “qwerty”.

With this we have finished the subject of functions, so from now on we will dedicate ourselves to other interesting issues, such as .

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