REALLY basic functions problem

Sorry, if I wasn’t clear

I didn’t say to delete the function.

Just delete the parameter

so in the line void getMIN(int MIN){ delete int MIN

Then call it with getMin();

Explanation

When you use void getMIN() { without the parameter, the variable MIN used in the function is just the global variable MIN that you already have. Good. It will impact what happens in setup and draw!

WITH the parameter the function uses the local parameter MIN and changes it without changing the global variable. Bad. We say the local variable / parameter is overshadowing the global variable MIN with the same name.

In both cases the function doesn’t return anything. Which is OK, when we change the global variable.

Alternatively

Alternatively (when the function should return anything) use an expression like MIN = getMin(); in setup().
Then the function looks slightly different:

  • at the beginning int getMIN() { (telling us what it is returning ) and
  • at its end return MIN; (returning MIN).
  • and also, it should declare a local variable MIN it returns. Would be best with another name than the global variable MIN, e.g. MIN_local or so.

I got both versions running now. Looks cool.

Remark

Actually what I would pass as a parameter is the array a.

E.g. void getMIN(int[] arrayLocal) {.................................

Chrisir

4 Likes