What is the role of the parameters? [duplicate]

7

I know the concept of função and procedimentos , but I have a little difficulty understanding the role of parameters .

    
asked by anonymous 21.09.2017 / 22:26

1 answer

6

Parametrize information, make something generic that would be specific, give variability to an algorithm with unknown external data at that location, is to establish a point of communication between the local algorithm and the external algorithm.

The parameter is a local variable like any other, at least in most languages or all mainstream , whose value will be known only in the function call. So the assignment of its value is always done at runtime, save some optimization that succeeds in eliminating it.

It has scope, lifetime, and visibility , so it only exists within the role while it is being executed. The only difference to the variable is the time of the assignment of the value that occurs in the call and not inside the function.

Note that the parameter exists in the function, unlike the call where a argument is passed. An argument can already be any valid expression in most cases. It can be a variable, a literal, an expression with formulas, etc. But you have arguments that need to be variable, it depends on typing.

In general parameters need to be validated since the received value may not be suitable for the algorithm . But it depends on the language, the typing discipline, the programming style.

Passing parameters has cost and should not be abused. Each of them has a copy of data, even the pro types refer to need to copy the address of the pointer.

Abuse can also occur from an organizational point of view. It is very complicated to administer functions with many parameters.

Just as you can receive parameters ( dictionary ) you can communicate leaving the function with the return. Usually it can only have a return, although some languages use implicit or explicit tuples to return more of a given. The communication in the output can occur by a parameter as well, as long as it is a reference and allows the data to be modified, then it enters with a value and when it is finished it is with another value. Obviously this will only be useful in the call if it is a variable.

This can be useful: What happens when we call a function?

    
21.09.2017 / 22:31