You need to add this line at the top of your function:
OPTIND=1
Otherwise, the sequential call of the function in the shell does not return it back, since the function is launched in the same shell every time.
According to help getopts:
, , getopts shell variable $name, , , , OPTIND. OPTIND 1 , script.