Having trouble with function returns

I’m trying to poll two switches.

The first switch will toggle between options and the second one will be used to drop out of the loop and ‘save’ the selected option.

I’m running into an issue while compiling: “kegVolume was not declared in this scope” and is highlighting the line “return kegVolume;”

I’m thinking that although I’m saving a value to int kegVolume, the data is not passing through to the return line. How can I pass this data through?

This is a function being called in the setup of my program as it only needs to run during initial start up. After which the variable kegVolume will be used to perform math (current volume - kegVolume). I’m sure this will lead to a similar issue as the “math” portion of my program is also a separate function but I’m confident that if the first issue is fixed I can apply the same logic to the next one to come up.

This is the function I’m working with:

int kegSetup()
{
  lcd.clear();				//clear the LCD display
  lcd.setCursor (1,0);			//set cursor to second column of first row
  lcd.print ("Style of Keg??");		//print question to LCD display
  lcd.setCursor (0,1);			//set cursor to first column of second row
  lcd.print ("1 = KEG 2 = SAVE");	//print options to answer question
  int runmode1 = 0;			//preperation for loop
  while (runMode1 = 0)			//set up loop to wait on button press
  {
    int switch1 = digitalRead(sw1);     //poll switch 1
    if (switch1 = 0)			//if KEG is selected
      {
	delay (500);			//prevent accidental multiple selections
        kegSelect++;			//increment kegSelect by 1
	if (kegSelect > 3)		//if kegSelect is greater than 3
	  {
	    kegSelect = 0;		//reset kegSelect to 0
	  }
	if (kegSelect = 0)			//keg selection option
	  {
	    int kegVolume = 1984;		//kegVolume = 1984oz. (15.5Gal)
	    lcd.clear();			//clear the LCD display
	    lcd.setCursor (0,0);		//set cursor to first column & row	
	    lcd.print ("1/2 Keg -> 15.5G");	//print current keg selection to LCD
	    lcd.setCursor (0,1);		//set cursor to first column second row
  	    lcd.print ("1 = Keg 2 = Save");	//print options to answer question
	  }
	if (kegSelect = 1)			//keg selection option
	  {
	    int kegVolume = 992;		//kegVolume = 992oz. (7.75Gal)
	    lcd.clear();			//clear the LCD display
	    lcd.setCursor (0,0);		//set cursor to first column & row
	    lcd.print ("1/4 Keg -> 7.75G");	//print current keg selection to LCD
	    lcd.setCursor (0,1);		//set cursor to first column second row
  	    lcd.print ("1 = Keg 2 = Save");	//print options to answer question
	  }
	if (kegSelect = 2)			//keg selection option
	  {
	    int kegVolume = 661;		//kegVolume = 661oz. (5.2Gal)
	    lcd.clear();			//clear the LCD display
	    lcd.setCursor (0,0);		//set cursor to first column & row
	    lcd.print ("1/6 Keg -> 5.2G");	//print current keg selection to LCD
	    lcd.setCursor (0,1);		//set cursor to first column second row
  	    lcd.print ("1 = Keg 2 = Save");	//print options to answer question
	  }
	if (kegSelect = 3)			//keg selection option
	  {
	    int kegVolume = 1690;		//kegVolume = 1690oz. (50L)
	    lcd.clear();			//clear the LCD display
	    lcd.setCursor (0,0);		//set cursor to first column & row
	    lcd.print ("EUR Keg -> 50L");	//print current keg selection to LCD
	    lcd.setCursor (0,1);		//set cursor to first column second row
  	    lcd.print ("1 = Keg 2 = Save");	//print options to answer question
	  }
        return kegVolume;      /*<<<<LINE IN QUESTION<<<<<*/
        int switch2 = digitalRead(sw2);         //poll switch 2
	if (switch2 = 0)			//if SAVE is selected
	  {
	    EEPROM.write (0,kegVolume);		//store kegVolume to EEPROM address 0
	    runMode1 = 1;			//drop out of loop
	  }
      }
  }
}

I can add the entire code if necessary for help.

I just realized I didn't declare the variable as a Global variable. That just solved all my issues.

Thanks anyway guys!

You declare kegVolume within a set of {}. Once the code executes past the }, kegVolume goes out of scope, and effectively ceases to exist.

Regards, Ray L.

 return kegVolume;      /*<<<<LINE IN QUESTION<<<<<*/
        int switch2 = digitalRead(sw2);         //poll switch 2
	if (switch2 = 0)			//if SAVE is selected
	  {
	    EEPROM.write (0,kegVolume);		//store kegVolume to EEPROM address 0
	    runMode1 = 1;			//drop out of loop
	  }
      }
  }
}

Why do you have more code beyond the return? You know what return does right? It ends the function. All this switch2 business and the EEPROM write will never ever happen.

  while (runMode1 = 0)         //set up loop to wait on button press

Why are you assigning a value to runMode1 in the while statement?

You do not want kegVolume to be a global variable.

However, you do want to declare it at an appropriate place inside the function, where it will be in scope at the point of of the return statement.

PaulS:   while (runMode1 = 0) //set up loop to wait on button press

Why are you assigning a value to runMode1 in the while statement?

Should I not be? Perhaps I'm mistaken in how while loops work.

michinyon: You do not want kegVolume to be a global variable.

However, you do want to declare it at an appropriate place inside the function, where it will be in scope at the point of of the return statement.

I think I don't need the return after all. I just want kegVolume affected or changed by what happens in that function. By declaring it as a global value I should be able to modify kegVolume within that function and have it stay to whatever value it's assigned, no?

Should I not be? Perhaps I'm mistaken in how while loops work.

Are you assigning the value there? Or do you mean to test it?

while(runModel == 0)

makes sense.

while(runModel = 0)

does not.

PaulS: Are you assigning the value there? Or do you mean to test it?

while(runModel == 0)

makes sense.

while(runModel = 0)

does not.

I'm testing it. I assigned the value of runMode1 in the previous line. I think I see your point now. Just one '=' is for assigning a value whereas two would be for a comparison.