# REALLY basic functions problem

**URL:** https://discourse.processing.org/t/really-basic-functions-problem/12794
**Category:** Coding Questions
**Created:** [July 18, 2019, 5:27pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794 "2019-07-18T17:27:39Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![EmptyAtoms](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@EmptyAtoms](https://discourse.processing.org/u/EmptyAtoms)
#### Post date: [July 18, 2019, 5:27pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/1 "2019-07-18T17:27:39Z")

</div>

Hi all,

So I’ve been given an assignment to produce a function that gets the lowest value of an array, then stores that value and uses it to color a circle, and write that same value onto that circle.

I cannot get my function to work. It seems to do nothing, commenting it out makes no impact whatsoever.

I am basically losing the will to live here as the person giving my class this assignment hasn’t given us any outlines of syntax and I am clearly ordering things or calling things wrong here.

If you can help, I would HUGELY appreciate it.

```auto
int a[]= new int [50];
int MIN= 0;

void setup()
   {
  size(450,450);
  
  for(int i=0; i < a.length; i++)
      {
  a[i] = (int) random (60,255);
print(a[i]+" , ");
println("MIN+",MIN);
      }
     }

  
 void draw(){
   fill (MIN);
   textSize(width/4);
   ellipse(width/2,height/2,width/2,height/2);
   
   fill (240);
   textSize(width/4);
   text(MIN, width*.32 ,height*.58);
   
   getMIN(8);
   
   }

void getMIN(int MIN){
 for(int i=0;i<a.length; i++)
   {
    if(a[i]< MIN){
      MIN=(a[i]);
      }
   }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 5:42pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/2 "2019-07-18T17:42:48Z")

</div>

You are almost there

Try to call getMin without a parameter (so in the line `void getMIN(int MIN){`  
**delete** `int MIN`) and call getMin at the end of setup ()

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 5:46pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/3 "2019-07-18T17:46:07Z")

</div>

Also since you check if the values in the array are \< MIN I advise to give MIN a very high value initially so the condition is met !

Welcome to the forum!

---

<div class="post-metadata">

### Author: ![EmptyAtoms](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@EmptyAtoms](https://discourse.processing.org/u/EmptyAtoms)
#### Post date: [July 18, 2019, 5:47pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/4 "2019-07-18T17:47:33Z")

</div>

Oh yeah I’ve less of a problem just running it within the setup.

But this is the brief,

"• Write a program which has a global array of 50

integers with random values assigned to the array.

• The values should be displayed in the console window in a row

• Write a function ‘getArrayMin’ to return the minimum value in the array.

• Draw a circle in the centre of the window with the  
greyscale colour returned by the getArrayMin function.

The circle should be 50% the size of the window

• The value returned should be displayed inside the  
circle and be at least 50% the size of the circle

• The value should be visible whether the circle is black or white"

So I have to do it in a function. For example, this works and I think it’s what you’re describing…

int a[]= new int [50];  
int MIN= 255;

void setup()  
{  
size(450,450);

for(int i=0; i \< a.length; i++)  
{  
a[i] = (int) random (60,255);  
print(a[i]+" , ");  
println(“MIN+”,MIN);  
if(a[i]\< MIN){  
MIN= a[i];

```
  }
  }
 }

```

void draw(){  
fill (MIN);  
textSize(width/4);  
ellipse(width/2,height/2,width/2,height/2);

fill (240);  
textSize(width/4);  
text(MIN, width\*.32 ,height\*.58);

}

When I try to use “int” as the return type and say “return (MIN);” it says “This method must return a result of type int”

I really have no idea how to use these things.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 5:49pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/5 "2019-07-18T17:49:50Z")

</div>

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

---

<div class="post-metadata">

### Author: ![EmptyAtoms](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@EmptyAtoms](https://discourse.processing.org/u/EmptyAtoms)
#### Post date: [July 18, 2019, 6:15pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/6 "2019-07-18T18:15:07Z")

</div>

Chrisir,

You’re a superhero. Thanks so much. I did what you said and it works. Just on your alternative suggestion, that seems to be fitting the brief much better. That was what I wanted to use initially, but when I try to use the “int” as the returnType (and say “return (MIN)”) it tells me “This method must return a result of type int”.

Also, how would I call the variable MIN\_local, if this was the way I was doing it? Am I close with this?  
It’s saying “MIN\_local cannot be reolved to a variable”.

```auto
int a[]= new int [50];
int MIN= 990;

void setup()
{
  size(450, 450);

  for (int i=0; i < a.length; i++)
  {
    a[i] = (int) random (90, 255);
    print(a[i]+" , ");
// println("MIN+", MIN);
  }
  
MIN = getArrayMin();

if (MIN>40){
  fill(0);
}

}

void draw() {
  fill (MIN);
  textSize(width/4);
  ellipse(width/2, height/2, width/2, height/2);

  fill (240);
  textSize(width/4);
  text(MIN, width*.28, height*.58);

}

int getArrayMin() {
  for (int i=0; i<a.length; i++)
  {
    if (a[i]< MIN) {
      MIN_local=(a[i]);
      return (MIN_local);
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 6:16pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/7 "2019-07-18T18:16:39Z")

</div>

declare the variable `MIN_local` locally at the start of the function

(your if-clause in the function read `if (a[i]< MIN) {` - can you spot the error)

```auto

int getMIN() {

  // declare the variable locally at the start of the function 
  int MIN_local=11000; 

  for (int i=0; i<a.length; i++) {
    if (a[i] < MIN_local) {
      MIN_local = a[i]; 
    }//if
  }//for
  return MIN_local;
}//function

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 6:26pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/8 "2019-07-18T18:26:48Z")

</div>

> [@EmptyAtoms](#):
>
> value should be visible whether the circle is black or white"

It’s a to do

**Remark**

Try the random between 110 and 222:

text will exceed the ellipse

---

<div class="post-metadata">

### Author: ![EmptyAtoms](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@EmptyAtoms](https://discourse.processing.org/u/EmptyAtoms)
#### Post date: [July 18, 2019, 6:30pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/9 "2019-07-18T18:30:45Z")

</div>

"Try the random between 110 and 222:

text will exceed the ellipse"

I was going to try get fancy with it so I can say:

```auto
  if (MIN>60){
  fill(0);
} else{
  fill(255);
}

```

But putting that in the draw function doesn’t seem to be working. Maybe I’m just framing it long.

I’m just trying to get a sense of how to express these sorts of terms even just reading your outline there gives me the right words to google and helps the syntax understanding a lot. Thanks again.

```auto
int a[]= new int [50];
int MIN= 255;

void setup()
{
  size(450, 450);

  for (int i=0; i < a.length; i++)
  {
    a[i] = (int) random (255);
    print(a[i]+" , ");
// println("MIN+", MIN);
  }
  
//MIN = getArrayMin();

  if (MIN>60){
  fill(0);
} else{
  fill(255);
}

}

void draw() {
  fill (MIN);
  textSize(width/4);
  ellipse(width/2, height/2, width/2, height/2);
  

  fill (240);
  textSize(width/4);
  text(MIN, width*.28, height*.58);

}

int getArrayMin() {
    int MIN_local=11000; 
  for (int i=0; i<a.length; i++)
  {
    if (a[i]< MIN_local) {
      MIN_local= a[i] ;
    }
  }
  return MIN_local;
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 6:35pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/10 "2019-07-18T18:35:52Z")

</div>

**Remark 1**

why did you comment this out:  
//MIN = getArrayMin();

won’t work (I understand why, to test the if-clause)

**Remark 2**

That’s a duplicate: textSize(width/4);

**Remark 3**

This works:

```auto
 if (MIN>60) {
    fill(0);
  } else {
    fill(255);
  }

```

BUT it must be directly before the `ellipse` command because at the moment, there is another `fill` command **after** this if-clause which overwrites the last `fill` value…

- Rule of thumb: Always do fill and stroke in `draw()` not in `setup()` and **before** using the ellipse or rect or text command that has to get this color.

**Remark 4**

The textSize for numbers MIN with 3 digits is another issue than the fill color.

**Remark 5**

Remember to hit **ctrl-t** in processing for auto-format of your code (indents…).

Chrisir

---

<div class="post-metadata">

### Author: ![EmptyAtoms](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@EmptyAtoms](https://discourse.processing.org/u/EmptyAtoms)
#### Post date: [July 18, 2019, 6:46pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/11 "2019-07-18T18:46:19Z")

</div>

" **Remark 1**

why did you comment this out:  
//MIN = getArrayMin();"

Just to make the circle white®. I’ll uncomment it after I’m happy with my if/else statement but at the moment it’s not working for me.

" **Remark 2**

That’s a duplicate: textSize(width/4);"

So it is! thanks!

So what I have now is this…

```auto
int a[]= new int [50];
int MIN= 59;

void setup()
{
  size(450, 450);

  for (int i=0; i < a.length; i++)
  {
    a[i] = (int) random (255);
    print(a[i]+" , ");
// println("MIN+", MIN);
  }
  
//MIN = getArrayMin();

}

void draw() {
  fill (MIN);
  ellipse(width/2, height/2, width/2, height/2);
  
  if (MIN<60){
  fill(255);
} else{
  fill(0);
}

  textSize(width/4);
  text(MIN, width*.28, height*.58);

}

int getArrayMin() {
    int MIN_local=11000; 
  for (int i=0; i<a.length; i++)
  {
    if (a[i]< MIN_local) {
      MIN_local= a[i] ;
    }
  }
  return MIN_local;
}

```

Seems to be working! Thanks again Chrisir you have honestly made such a big difference to my day. I couldn’t be more grateful.

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [July 18, 2019, 6:52pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/12 "2019-07-18T18:52:11Z")

</div>

> [@EmptyAtoms](#):
>
> I am basically losing the will to live here

@Chrisir another soul saved 😉

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [July 18, 2019, 6:59pm UTC](https://discourse.processing.org/t/really-basic-functions-problem/12794/13 "2019-07-18T18:59:45Z")

</div>

this

```auto
 if (MIN<60) {
    fill(255);
  } else {
    fill(0);
  }

```

is changing the color of the text (not of the ellipse) - which is ok

My version

- intially MIN can be 0, it gets overwritten; MIN\_local must have a big value initially though.

- passing **“`a`”** as a parameter now. Therefore it can be moved into setup() and not be of global scope anymore.

- using `textAlign(CENTER, CENTER);` and `text(MIN, width/2, height/2);`

Chrisir

```auto
int MIN = 0;

void setup() {
  size(450, 450);

  int[] a = new int [50];
  for (int i=0; i < a.length; i++) {
    a[i] = (int) random (255);
    print(a[i]+", ");
  }
  MIN = getArrayMin(a);
}//func

void draw() {
  // show ellipse
  fill (MIN);
  ellipse(width/2, height/2, width/2, height/2);

  // show text
  if (MIN<60) {
    fill(255);
  } else {
    fill(0);
  }
  textSize(width/4);
  textAlign(CENTER, CENTER); 
  text(MIN, width/2, height/2);
}//func

// ------------------------------------------------------

int getArrayMin(int[] a) {
  int MIN_local=11000;
  for (int i=0; i<a.length; i++) {
    if (a[i]< MIN_local) {
      MIN_local = a[i];
    }
  }
  return MIN_local;
}//func

```
