Parameter of a function

Asked

Viewed 60 times

1

How the code ran right if the function parameter experiencia does not have the same variable name anosEstudo. How Javascript understands that they are the same thing?

<script>
        var anosEstudo = 1;

        function experiencia(anos) {
            if (anos <= 1) {
                return 'Iniciante';
            } else if (anos <= 3) {
                return 'Intermediário';
            } else if (anos <= 6) {
                return 'Avançado';
            } else {
                return 'Jedi Master';
            }
        }

        console.log(experiencia(anosEstudo));
    </script>

inserir a descrição da imagem aqui

1 answer

4


They are not the same thing, they are completely different things. Even if they had the same name, they would be different things. To understand more about this you should see What is the difference between scope and lifetime?.

You should probably read it too What is a variable?. And you must follow all links of these answers to learn the concepts correctly and not just follow cake recipes, because following recipes is not programming. You were right to ask, it’s rare for that to happen.

When you call a function and pass a argument is diverting the code execution flow within the function and copying the arguments to the function parameters.

So in this case you are copying the value that is in the variable anosEstudo for the local variable within experiencia which is called anos.

The idea is to isolate one variable from the other. Even if the parameter called anosEstudo would still be another variable and different scope. Good. Imagine running a system with thousands or millions of variables where they’re all visible everywhere, and if you had the same name, it would be the same variable. It’d be crazy, you can’t.

The most commonly used technique in programming, which most programmers ignore, even when they apply it without realizing it, is Divide and Conquer. And that goes for everything in life, to work, to study, to do any complex activity. We don’t do everything together. Isolating parts is fundamental, and they must talk for a few details there cannot be an unintentional communication. You can’t take the die without being what you want.

I’ll tell you what, the way this code is written gives you the wrong idea, the right thing would be this:

function experiencia(anos) {
    if (anos <= 1) return 'Iniciante';
    else if (anos <= 3) return 'Intermediário';
    else if (anos <= 6) return 'Avançado';
    else return 'Jedi Master';
}

var anosEstudo = 1;
console.log(experiencia(anosEstudo));

Because then it becomes clear that the variable is part of the bottom algorithm that is completely isolated from the function, by chance the two things are together, but they might not be.

The way it is in the question it seems that the variable anos Estudo needs to exist before the function to work, but this is not the case, it is not seen by the function, even though it was declared before, could be even in another file. The communication of the data could be by copy.

In fact the variable is not even necessary because what counts is the value:

function experiencia(anos) {
    if (anos <= 1) return 'Iniciante';
    else if (anos <= 3) return 'Intermediário';
    else if (anos <= 6) return 'Avançado';
    else return 'Jedi Master';
}

console.log(experiencia(1));

I put in the Github for future reference.

There are cases that need a variable, but this is more advanced and I will not talk here.

Now I’ll show you something that works, but it shouldn’t:

function experiencia() {
    if (anos <= 1) return 'Iniciante';
    else if (anos <= 3) return 'Intermediário';
    else if (anos <= 6) return 'Avançado';
    else return 'Jedi Master';
}

var anos = 1;
console.log(experiencia());

Yes, the external variable can be seen by the function (as long as it is in the same place where the function is, in this case it is the global scope), and this is almost an error of the language. Not so much, because there is a situation that is interesting. But not in this case. Except for a concept of closure which I will not mention here, do not make function code that depends on external information without communication by parameter. This breaks the isolation of the function and is usually not what you want to do, even if it seems easier is complicated to administer in slightly larger codes.

I find it useful to read Parameters in Javascript functions.

  • Thank you! About the part of the code giving you the wrong idea: in fact, in the original it’s the way you put it, I was the one who altered it, out of ignorance. But, I did not understand what would be the problem in putting the variable before. If I could clear up more, I would really appreciate it! But from now on, thank you very much!

  • Beauty, they are completely different things. So how do I report that the value of yearsStudy is 1 and this is recognized in the function experience? Of course, I know it has something to do with the last line of the code, where I call the function with the yearsStudy parameter, but I can’t understand how it works and what’s happening there.

  • It’s no problem, but it gives you the wrong idea. Now from the comment, it looks like you used it but you don’t know what parameter passage is. Did you read everything in the answer? Followed the links. If you still don’t know you’re probably learning the wrong way and you’re skipping steps, it doesn’t work, I would suggest taking things simpler before, and learning in a structured way, there’s no way to make the roof of a house without a foundation.

  • Man, thanks for the tips, I went to study more as Oce said and now I understand much more! But then there was another question: if I wanted to use the same variable yearsStudy as a function parameter, could I without problems? Or would I be wrong?

  • I don’t think you understand. Or I don’t understand.

Browser other questions tagged

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