Cannot exit from a function?

Hi,
Is there a way to exit from a subcommand, which is a function in my example below?

 
#!/bin/ksh
 
function exitFunction {
    if [[ $1 -eq 1 ]]; then
        echo "success"
    elif [[ $1 -eq 0 ]]; then
        echo "failed"
        exit 1    # the exit problem
    fi
    exit 0
}
 
testVar=`exitFunction 1`
echo $testVar
 
testVar2=`exitFunction 0`
echo $testVar2
 
echo "this message should not be shown."
exit 0

The result of my script:

 
success
failed
this message should not be shown.

It should exit right after the "failed" message, but it it's not.

I appreciate your help.

You execute your function in a subshell (`...`) so it exits from there into the current shell
If you execute your function like this:

exitFunction 1
echo $?

you will exit from the script. You can do something like this:

testVar=`exitFunction 1`
test $? -ne 0 && exit 1

That is a wasted fork.

There's a special syntax for exiting from a subroutine, called return. It works like exit, it can take a value, but it doesn't end the script.

return is also used to exit from a sourced script.