Minimum value of an array

Hi, I found a program for picking out the lowest value in an array. I can't seem to be able to get to the return value which should be 24. Printing the parameter attached to the return is not coming out on the serial print out. Had no objections to my coding after a few tries.
Anybody know how I can get the 'return' result?
Here is the code;
int array[6] = {25, 27, 24, 28, 26};
int getMin(int* array, int size);

void setup() {
Serial.begin(9600);
}
void loop() {

int minIndex = 0;

int min = array[minIndex];
for (int i=1; i<6; i++){

if (array[i] < min){
minIndex = i;
min = array[i];
}
}
return minIndex;
Serial.println(minIndex);
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

  return minIndex;    //unconditionally end the loop() function and return to where it was called
  Serial.println(minIndex); //so this code never gets executed
}

Reverse the order of the lines, but why are you using return anyway ?

The minimum value in your array is zero, reason being that you declare the array to have 6 elements but you only assign 5 elements and that cases the compiler to fill the 6th element with a zero. Another problem is that you are accessing the elements 1..5 leaving out the element at index 0.

EDIT: Element at index 0 is used as base for comparisons.

You were close! sketch.ino - Wokwi Arduino and ESP32 Simulator

Try.

How many?

I have reversed the order of the lines Bob. It is now printing '5'. The lowest value is 24. So this is wrong. I am using return because this is a difficult to understand program which I found on the forum somewhere. I therefore wanted to copy it out exactly as given.
Therefore there is still something wrong with the program or the 24 is there somewhere and I need to find it

Look up arrays in the help reference. You have to add one more element than the actual number in the array. Something about a null element.

Look up arrays in the help reference. You have to add one more element than the actual number in the array. Something about a null element.

Looked up your sketch reference. It wouldn't work in my case. The elements of the array are determined in void loop. All of the sketches I have seen so far relating to this don't take place in the void loop. This would not be my complete sketch. The partial sketch should be returning 24. When interchanging the 'return' and 'print' lines as suggested I am getting the number 5. Interesting that this happens to be the number of elements in the array.
"return" should be giving me the minimum number but where do I find the actual value of the return?

No you don’t

No surprise really as you are printing minIndex, not the minimum value

Try this

int array[] = {25, 27, 24, 28, 26}; //allow the compiler to set the size of the array
byte NUMBER_OF_ELEMENTS = sizeof(array) / sizeof(array[0]); //calculate the ni,ber of array elements

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  int min = 32000;  //start with a huge value
  for (int index = 0; index < NUMBER_OF_ELEMENTS; index++)
  {
    if (array[index] < min)
    {
      min = array[index]; //save the new minimum *value*
    }
  }
  Serial.println(min);  //print the miniumum value
}

Note how it gets the compiler to do some of the work

You are confusing 2 different things. If you want to store text in an array then you need one extra character at the end for the null terminator, which is zero (0x00). The zero tells any code using the text where it ends. "Hello" for example requires 6 places in an array.

You are not storing text, you are storing numbers. You need 1 place for each number, so 5 in the case of the examples you've given, which will be indexed from 0 to 4.

I do not need to look up anything, I know how it works. You are referring to c-strings (aka char arrays) which usually are null-terminated but that does not apply to binary arrays. You go look that up.

C-strings are always null terminated. Without the termination an array of chars is not a C-string

Yes I can see I was wrong. I have fixed it now. Thanks for that. The reference in help in Arduino was not clear on that.

Yes I have it now. Program now working fine to pick out lowest number in a series of 5 numbers. Had to get rid of a few things in the example program I found that just weren’t necessary at all and probably stopped it working. Didn’t need “ int getMin(int* array, int size);” in the declaration. Or is it possible that this single line of code will get the minimum of the array? This is simpler than using the “for” statement. That is the strangest coding I have ever seen. Is that a multiply sign before array? If that does work where would the result be showen? How exactly would that fit into the coding?

It's a function prototype.
The Arduino IDE makes them automatically for you, but they are a part of C/C++ for forward references.
The '*' operator used here monadically means the variable is a pointer.

Yes, that is correct. What I meant was that a c-string may contain a list of strings / lines where each string is terminated by a '\n' and the entire list is terminated by '\0'. In Windows API you may have string lists where each string is terminated by '\0' and the list is then terminated by an empty string which results in a double null termination '\0\0'.