Is there any way I can call an instance of a function repeatedly with the function retaining internally the last state of the GPIO pin

This thread has a few suggestions you might find helpfuL: In Julia, how to create a function that saves its own internal state?