That your first attempt doesn't work is logical since changing i and j variables within the function doesn't affect the parameters that were used to set these variables.

Your second attempt looks fine to me.
What exactly isn't working ?