When executing a bash shell script in ubuntu 18.04, the location parameter doesn't work, how can I solve this problem?

$# in your function contains count of arguments passed to function, not to whole script. You could execute sethostname "$@" to pass all script arguments to function, then arguments count will work as you've expected.