Your function uses global variables, which is bad for several reasons:
The variables must be reset before the function can be called again.
The variables can be modified from outside of your function,...
That is not the Sieve of Eratosthenes
The Sieve of Eratosthenes computes multiples
of each found prime to mark subsequent composite numbers in the sieve.
Your algorithm computes the remainder of all ...
This post from Martin R is a good step in the right direction toward generalizing your problem.
The helper function at the top of the post is already what you need for making a more generalized loop: