The parameter name does not match the last variable

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);
}

Is there anything wrong with the syntax / writing of the above code?

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

    
asked by anonymous 14.11.2017 / 04:33

1 answer

2

The function is completely independent of your 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 receive what it turns out, you do not need to know extra details of how it was written to use it. The name of the parameters is not part of the contract, you do not 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 language expressions, including literals or calculations made there, you need to use variables.

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

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

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

See What is the difference between parameter and argument? .

    
14.11.2017 / 11:13