Are there any experts in Bash script here that can help?

I'm writing a script to run in the Task Scheduler in my Synology NAS, but my programming pattern for one particular part of it is giving me fits. The "exit" command in Bash behaves in a way that I would call "weird" compared to what I would expect.

In one situation, "exit" behaves more like a "return" statement. Clearly it's because Bash is similar to Powershell, which I have much more experience with, and I'm expecting Bash to behave like Powershell here, but it doesn't. So clearly I'm doing this wrong because I have incorrect expectation, but in this case, what is the right way to do this programming pattern instead?

I've done some googling, and everyone says "exit should exit the program, period". But that's not what's happening when I try it in this one particular situation.

Below is the example script. I'm hoping that it's self-explanatory. Thanks in advance for any help or suggestions.

Code:

#!/bin/bash

# ----------------------------------------------
# Program to demonstrate an issue with Bash.
# ----------------------------------------------

# ----------------------------------------------
# Function: Test for a problem. If there is no
# problem, then gather some data and return it.
# If there is a problem, exit the program.
# ----------------------------------------------
TestForBadProblem()
{
  echo "Testing for a potential bad problem..."  >&2
  if "$badProblem" = true
  then
    echo "There was a bad problem. Exiting program now." >&2
    exit 1
  else
    echo "No bad problem found. Returning data from function." >&2
  fi

  returnData="Some_Data"
  echo $returnData
}

# ----------------------------------------------
# Main program code body
# ----------------------------------------------
echo "Starting program now."  >&2

# ----------------------------------------------
# First test - There should be no problem and it
# should return data from the function.
# ----------------------------------------------
echo "First test - No problem encountered, data retrieved." >&2
badProblem=false
returnedData=$( TestForBadProblem )
echo "returnedData was: $returnedData" >&2

# ----------------------------------------------
# Second test - There should be a bad problem
# encountered and it should quit the program
# without trying to return any data at all.
# It shouldn't even reach the line that tries
# to echo the returned data.
# ----------------------------------------------
echo "Second test - Problem encountered, attempt to retrieve data, method 1." >&2
badProblem=true
returnedData=$( TestForBadProblem )
echo "returnedData was: $returnedData" >&2

# ----------------------------------------------
# You should not reach this line of code because
# the program should have exited in the second
# test. However the program continues to this
# point, despite the documentation for "exit"
# indicating that it should truly exit rather
# than just act like a "return" statement.
# ----------------------------------------------
echo "----------------------------------------------------------------" >&2
echo "BUG: If you can read this, the program did not exit as expected." >&2
echo "----------------------------------------------------------------" >&2

# ----------------------------------------------
# Third test. Try a different way of reading the
# function's return data. This does not work, it
# just sets a string value rather than calling
# the function. The function never gets called.
# ----------------------------------------------
echo "Third test - Attempt to retrieve data, method 2." >&2
badProblem=true
returnedData=TestForBadProblem
echo "returnedData was: $returnedData" >&2

# ----------------------------------------------
# Last test - Call the function but without
# trying to read any return data from it. This
# works as expected, but I want to read the data
# so I can't use this method.
# ----------------------------------------------
echo "Last test - Problem encountered, do not attempt to retrieve data." >&2
badProblem=true
TestForBadProblem
echo "Program will not reach this line, you will not see it." >&2





The output from the above program looks like this:



Code:
Starting program now.
First test - No problem encountered, data retrieved.
Testing for a potential bad problem...
No bad problem found. Returning data from function.
returnedData was: Some_Data
Second test - Problem encountered, attempt to retrieve data, method 1.
Testing for a potential bad problem...
There was a bad problem. Exiting program now.
returnedData was: 
----------------------------------------------------------------
BUG: If you can read this, the program did not exit as expected.
----------------------------------------------------------------
Third test - Attempt to retrieve data, method 2.
returnedData was: TestForBadProblem
Last test - Problem encountered, do not attempt to retrieve data.
Testing for a potential bad problem...
There was a bad problem. Exiting program now.
_________________________
Tony Fabris