As mentioned by others, the parens create a subshell and the exit or return command instructs bash to exit from the subshell. You should use curly braces. Note that you can use bash parameter substitution to get the same effect in a simpler way. See below.
Your line test $variable || (echo "Value of \$variable cannot be null."; exit 1)
should not work either even 'outside' of the context of a function. For instance:
$ cat t.sh
#!/bin/bash
a="hello"
test $a || (echo "Value of \$variable cannot be null."; exit 1)
echo First test passed
a=
test $a || (echo "Value of \$variable cannot be null."; exit 1)
echo Second test passed
gives:
$ ./t.sh
First test passed
Value of $variable cannot be null.
Second test passed
$
Where we see that the second echo
is run even though it shoulnd't have. Using curly braces is the way to go.
$ cat t.sh
#!/bin/bash
a="hello"
test $a || { echo "Value of \$variable cannot be null."; exit 1 ; }
echo First test passed
a=
test $a || { echo "Value of \$variable cannot be null."; exit 1 ; }
echo Second test passed
$ ./t.sh
First test passed
Value of $variable cannot be null.
$
Note that you could use the bash ${parameter:?err_msg}
construct for parameter substitution:
$ cat t.sh
#!/bin/bash
a="hello"
: ${a:?'a is null or undefined! Exiting..'}
echo First test passed
a=
: ${a:?'a is null or undefined! Exiting..'}
echo Second test passed
$ ./t.sh
First test passed
./t.sh: line 9: a: a is null or undefined! Exiting..
$