Parameters in Javascript functions

Asked

Viewed 328 times

1

What are the parameters in the functions and what are their needs?

I don’t understand the difference between having or not having a parameter in the function, for example

Parameter-less:

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 why use?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

6

It is a matter of scope. Every variable created in a scope is available for 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 function calcula() in another scope it will not work. Only works in this case because even with var in this case the variable takes a global scope (it could be regional, but in this case it is global. This type of scope is often problematic, and should be avoided, it should only be used when it is very important and very carefully.

Note that these variables V and R can even be changed within the function, and this is something dangerous. In a more complex code base have access to a variable with that name which 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 path. This code should be like this:

function main() {
    var V = prompt("Entre 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()

I put in the Github for future reference.

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()

I put in the Github for future reference.

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

Just because it works doesn’t mean it’s right.

Fiat 147 caindo aos pedaços circulando na rua

  • I think I get it, and this image there, it really makes sense KK, thank you

  • 1

    This image is already almost Rademark yours! : D I think it is one of the ones that highlights the very important point that made funcionar != certo

1

Seek to learn about global and local variable scope.

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");
  • About scope I understand the basics, business was more about the same parameters, I did not know it had to do with scope

  • @Stevealisson Some things are a matter of practice, with time becoming clearer ;)

  • To what extent should I learn Javascript? to the point that I can develop anything? (My interest is in website only, basically visual things)

  • @Stevealisson N stick to it, always be open to learning more ;)

  • Believe me, I’m always trying to learn more! Thanks for the help, hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.