Parameter name does not match with the variable passed

Asked

Viewed 60 times

1

void countdown(int max, boolean output) {
  for (int i=max; i>=0; i--)
    if (output)
      println(i);
}
void setup(){
  int top = 20;
  countdown(top, true);
}

There is something wrong with the syntax/writing of the above code?

The method Countdown is called with the variable top instead of the variable max. And Countdown is called with a literal boolean, not a variable.

  • 2

    Apparently it’s quiet. What bothers you?

  • The Countdown method is called with the top variable instead of the max variable. And Countdown is called with a literal Boolean, not a variable.

  • 1

    But this is the idea of differentiating parameters from arguments. Read more here

1 answer

2


The function is completely independent of its call. One of the ideas behind the concept of function is precisely this. Function serves as an abstraction, so you just need to know the minimum contract to call it and get what it results in, you don’t need to know extra details of how it was written to use it. The name of the parameters is not part of the contract (has language that does), you even need to know them, even if you know there is no obligation to use the same names in the call arguments, even because the arguments accept any expressions of the language, including literals or calculations done there, do not need to use variables.

The parameter is always a variable, the argument is always a value, if it is obtained through a variable is only a coincidence.

In Java the contract only matters the name of the function (method) and the types of all parameters in the order, plus the type of return that is not part of the signature of the method.

Has language that allows you to use the parameter name in the argument, thus giving more semantics to what is doing and even allowing to use out of order and leave some optional arguments.

Behold What is the difference between parameter and argument?.

Browser other questions tagged

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