Setting global variable values in setup()

I am trying to assign global variables values in setup() form Serial Monitor input data.
I have tried two ways.

  1. Through Serial Monitor transaction code in setup(). This works and values are available for loop().
  2. Through a function, called in setup(). This doesn't result in values being available for loop().

Is my code incorrect, or is this functionality?

Thanks,
Bob

// Define variables
int ledPin = 13;
int onDelayPeriod;  //For user input number of Milliseconds delay before LOW command
int offDelayPeriod; //For user input number of Milliseconds delay before HIGH command
String onDelayMsg ("How long do you want the light on for each blink? Enter milliseconds");
String offDelayMsg ("How long do you want the light off for each blink? Enter milliseconds");
String responseMsg ("You entered ");


void setup() {

  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
// This works
/*
  //Get User Input
  //Prompt User for Input    
     Serial.println("How long do you want the light on for each blink? Enter milliseconds"); 
  
  // Wait for User to Input Data  
  while(Serial.available()==0) {
  }
  
  //Read the data the user has input
  onDelayPeriod=Serial.parseInt();

    //Provide user with input value received
  Serial.print("You entered ");
  Serial.println(onDelayPeriod);
  
  //Prompt User for Input
  Serial.println("How long do you want the light off between blinks? Enter milliseconds"); 
  // Wait for User to Input Data  
  while(Serial.available()==0) { 
  }
 //Read the data the user has input
  offDelayPeriod=Serial.parseInt();
   
  //Provide user with input value received 
  Serial.print("You entered ");
  Serial.println(offDelayPeriod);
*/

//This doesn't work

 //Get User Input
  int onDelayPeriod = getIntData(onDelayMsg,responseMsg);
  Serial.print("onDelayPeriod is "); //For debug
  Serial.println(onDelayPeriod);  //For debug
  int offDelayPeriod = getIntData(offDelayMsg,responseMsg);
  Serial.print("offDelayPeriod is ");  //For debug
  Serial.println(offDelayPeriod);  //For debug
 
}

void loop() {
  Serial.print("onDelayPeriod is "); //For debug
  Serial.println(onDelayPeriod);  //For debug
  Serial.print("offDelayPeriod is "); //For debug
  Serial.println(offDelayPeriod); //For debug
  digitalWrite(ledPin, HIGH);
  delay(onDelayPeriod);
  digitalWrite(ledPin, LOW);
  delay(offDelayPeriod);
}

// Define functions

//This function is to get Integer data from the Serial Monitor

int getIntData(String msg1, String msg2)
{
  // Define local variable
  int  inputVal;
  
  //Prompt User for Input
  Serial.println(msg1);
   
  // Wait for User to Input Data
  while (Serial.available() == 0) {
  };

  //Read the data the user has input
  inputVal = Serial.parseInt();

  //Provide user with input value received
  Serial.print(msg2);
  Serial.println(inputVal);

  //Output the value to the global variable
  return inputVal;
int onDelayPeriod;  //For user input number of Milliseconds delay before LOW command
...
void setup() {
...
 //Get User Input
  int onDelayPeriod = getIntData(onDelayMsg,responseMsg);

You have two variables called onDelayPeriod - one globally and one in setup.

Lose the “int” in setup.

int onDelayPeriod;  //For user input number of Milliseconds delay before LOW command
...
void setup() {
...
 //Get User Input
  onDelayPeriod = getIntData(onDelayMsg,responseMsg);