"Void value not ignored as it ought to be" when using return with a function

I don’t have much C experience but I’m trying to get a function to return a value without success.

const int trigPin = 9; //trigger pin for ultrasonic sensor
const int buttonPin = 2;
const int echoPin = 10; //echo pin for ultraonic sensor
long duration; //duration for echo
int distance; //distance of water from sensor
int height[10]; //array for water reading
int targetHeight;
int buttonState = 0;
unsigned long setpoint; 
long previousMillis = 0;
long interval = 6000; //how often to call ping()
unsigned long sum;
unsigned long average;
unsigned long setPoint;

void setup(){
  pinMode(buttonPin, INPUT);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}

void loop(){
  unsigned long currentMillis = millis();
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    setLevel();
  }
    else if (currentMillis - previousMillis >= interval){
      previousMillis = currentMillis;
      ping();
      calculate();  
 }}

void setLevel(){
  ping();
  setPoint = calculate();
  Serial.println(setPoint);
}

void ping(){
  int i;
  for (i = 0; i < 10; i = i + 1){
    digitalWrite(trigPin, LOW); // Clears the trigPin
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH); 
    delayMicroseconds(10); // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound wave travel time in microseconds
    distance= duration*0.34/2; // Calculating the distance
    height[i] = distance;
  }
  //calculate();
}

void calculate(){
  sum = 0;
  int i;
  for (i = 0; i < 10; i = i + 1){
    sum += height[i];
    //Serial.println(height[i]);
  }
  //Serial.println("Sum:");
  //Serial.println(sum);
  average = (sum / 10);
  //Serial.println("Average:");
  //Serial.println(average);
  return average;
}

I’m trying to use calculate() to determine the distance from the ultrasonic sensor and I want to use the pushbutton to save a given distance as the setpoint. calculate() should return the average after ping() is called but it seems to be empty or else I don’t understand the error.

Void means "this function does not return a value". If you want to return an unsigned long then you must declare it as an unsigned long.

Where should I be declaring that? I declared average and setpoint as unsigned longs before setup().

Figured it out. Declare before the function name.

Just a stupid question from my side.

Why would you use a global variable ‘average’ and still return it from the function?

unsigned long calculate() {
  unsigned long sum = 0;
  unsigned long average;

  int i;
  for (i = 0; i < 10; i = i + 1) {
    sum += height[i];
    //Serial.println(height[i]);
  }
  //Serial.println("Sum:");
  //Serial.println(sum);
  average = (sum / 10);
  //Serial.println("Average:");
  //Serial.println(average);
  return average;
}

You can now get rid of average and sum that you declared in the beginning of your code because they are not used anywhere else.

You have two variables setpoint, one with uppercase ‘P’ and one with lowercase ‘p’. This is calling for trouble because it’s easy to make mistakes. You e.g. set the one with uppercase ‘P’ and later use the one with lowercase ‘p’ and can’t understand why it’s always zero.

Personally I try to use a minimal of global variables. It prevents coding ‘accidents’ where you by accident change the variable in a function where it was not supposed to be changed; if a variable has local scope (e.g. only known inside the calculate() function, there is no chance that you can change it by accident change in e.g. loop(). This can also save you a little bit of memory because the variables are removed from memory when the function ends so you have more space (if needed) in another function.