Why do we use parameters/arguments in functions?

Asked

Viewed 83 times

1

If a function can see environment variables, that is, it can see variables defined in the main program. Why we pass parameters and arguments to this function?

In the example below, I wrote an example in Python, in the first, I use the function without parameters and in the second, with parameters, and both present the same result.

def calculadora ():
    return n1 + n2 
n1 = int(input())
n2 = int(input())
print(calculadora())

def calculadora (n10,n20):
    return n10 + n20 
n1 = int(input())
n2 = int(input())
print(calculadora(n1,n2))
  • 2

    There is not only the global scope of the application, you will soon see that there are different scopes such as packages, classes, functions, etc., depending on the language you will use. Another point is that function serves to reuse code so, if you use the first way you did, you will always have to use the variables n1 and N2 as input of this function, which is bad because you will have to write these variables and so at the risk of overwriting some important data.

  • 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

7

Functions and parameters

Functions are not programming inventions. Contrary to what I understand people think, functions exist in mathematics, I don’t know why people don’t remember that.

Functions in mathematics have parameters to allow communication with those who need the function. There would be no reason why in programming adopt the mechanism of function and do different.

Remembering that in mathematics, in simple problems the function could take a die out of it, even because the paper accepts everything.

But of course it is not so in programming just because they decided to do the same, it is extremely useful that it is so.

Imagine if there were no parameters, the function would be of little use, it could only do something very simple, it could not do something based on a different situation at each call. Unless you pick up loose information on paper.

Then you show that it works without parameters. But this is an anomaly, it’s wrong. It is something that only languages that like side effects, which were thought to write small scripts, allow to do. Functions should not allow this kind of thing and the language that allows to misrepresent it. In serious programming, if the language allows the person to do two things: a) do not use the anomaly; b) change the language that is more judicious.

No problem and abuse it in scripts simple, but when one starts making serious systems he can’t even contemplate using something like this, it becomes a mess.

It is also no problem to capture a variable in a mechanism of closure, but then the scope is much more contained and has a motivation, is not gambiarra.

In important systems you get to have many variables, several paths to run and need to have control of everything in a well organized way, and for this the more the things are divided and contained, the better.

If you want to know more you can study:

Learn the right

Fiat 147 todo detonado andando pelas ruas

So learn the right concepts, don’t believe in what works, because it can be coincidence or it can even be acceptable in a certain scenario, but not in others. Learning based on trying and seeing the result is learning wrong.

Communicate only by parameter and return, except in many simple codes, and even then only after mastering the programming, because if you do this while learning you will get used to doing so, since it seems easier (and the price comes later, inexperienced programmers can’t see that), people like what seems easier and always get in trouble because of this.

Global variable dominates readability

It’s very easy to lose leverage over global variables, so don’t just use parameters, never use them, even if you can. Pretend the language doesn’t allow it.

n1 = 10
n2 = 20
def calculadora (n1, n2):
    return n1 + n2 
n1 = 10
n2 = 20
print(calculadora(1, 2))

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

What does this print? Looking over it is intuitive? Imagine it in larger code...

-4

Well, in the smallest possible response, the parameters capture local scope information, that situation where you call it within another function

Browser other questions tagged

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