What is the role of parameters?

Asked

Viewed 172 times

7

I know the concept of função and procedimentos, but I’m having a little trouble understanding the role of parameters.

1 answer

6


To parameterize information, to generic something that would be specific, to give variability to an algorithm with unknown external data at that location, is to establish a communication point 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, except for some optimization that can eliminate this.

He has scope, life time and visibility local, then exists only within the function, while it is running. The only difference for the variable is the time of assignment of the value that occurs in the call and not within the function.

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

In general parameters need to be validated since the value received may not be appropriate to the proposed 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 there is a data copy, even types by reference need to copy the pointer address.

Abuse can also occur from an organizational point of view. It becomes very difficult to manage functions with many parameters.

Just as you can receive parameters (dictionary) can communicate leaving the function with her return. It can usually have only one return, although some languages use implicit or explicit tuples to return more than one data. The output communication 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 finishes this 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?

  • A lambda expression v((x) -> x.y > 0) or an anonymous function v(fun(p) { }) can be considered an argument?

  • Yes, anything that can be passed to the function is an argument. Lambdas are functions, probably from first class.. In this case the parameter will be a reference to the function of the lambda and she can be called normally.

  • So the parameters are like a variable that will only be read and modified in a subroutine?

  • 2

    @Ordasivarov yes, it is a local variable that only exists inside.

Browser other questions tagged

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