Parameters in functions in JavaScript

1

What do the parameters in the functions serve and what do they need?

I do not understand the difference between having a parameter in the function, for example

No parameters:

var V = prompt("Enrte com o valor da tensão");
var R = prompt("Entre com o  valor da resistência");

function calcula() {
  var l = V / R;
  return l;
}

var corrente = calcula();
document.write("O valor da corrente é ", corrente, "A");

With parameters:

var V = prompt("Enrte com o valor da tensão");
var R = prompt("Entre com o  valor da resistência");

function calcula(V,R) {
  var l = V / R;
  return l;
}

var corrente = calcula(V,R);
document.write("O valor da corrente é ", corrente, "A");

If you have the same results to use?

    
asked by anonymous 18.03.2018 / 15:37

2 answers

6

It's a matter of scope . Any variable created in a scope is available to all scopes that are nested in this scope. That's why the first code works. Some variables are created with global scope even in more internal scopes and this is terrible and should be avoided, so always use var or let .

If you call the calcula() function in another scope it will not work. It works only in this case because even with var in this case the variable assumes a global scope (it could be regional, but in this case it is global even.This type of scope is usually problematic, and should be avoided, it should only be used when it is too important and very carefully.

Note that these V and R variables can even be changed within the function, and this is somewhat dangerous. In a more complex codebase have access to a variable with that name that is not even what you want.

Consider that both codes are wrong. They work, but it is not ideal to do so. You can even do it, but you have to know very well what you are doing, when you do not know it is better to follow the safest route. This code should look like this:

function main() {
    var V = prompt("Enrte com o valor da tensão");
    var R = prompt("Entre com o  valor da resistência");
    var corrente = calcula(V,R);
    document.write("O valor da corrente é ", corrente, "A");
}

function calcula(V, R) {
    return V / R;
}
main()

Now try doing without parameter:

function main() {
    var V = prompt("Enrte com o valor da tensão");
    var R = prompt("Entre com o  valor da resistência");
    var corrente = calcula(V,R);
    document.write("O valor da corrente é ", corrente, "A");
}

function calcula() {
    return V / R;
}
main()

The local scope of the two variables prevents them from being seen in the other function.

Just because it works does not mean it's right.

    
18.03.2018 / 15:52
1

Find out about global and local variable scope . / p>

Example:

var a = 1; // Esta disponivel para a função A e B
function A(){
    var somenteA = 11; // Esta disponivel somente para a função A

    console.log(a); // Mostra o valor de a, 1
    console.log(somenteA); // Mostra o valor de *somenteA*, 11
}

function B(){
    var somenteB = 12; // Esta disponivel somente para a função B

    console.log(a); // Mostra o valor de a, 1
    console.log(somenteB); // Mostra o valor de *somenteB*, 12
    console.log(somenteA); // Não consegue acessar o valor de *somenteA* pq ela foi declarada dentro da *function A*
}

A better way to write code:

function calcula(V,R) {
  return v/r;
}

document.write("O valor da corrente é ", calcula(5,5), "A");
    
18.03.2018 / 16:06