GSM Shield receive an SMS and reply

You don't need to execute the command

SIM900.print("AT+CNMI=2,2,0,0,0\r");

more than once, so move it to set up.

So, running through your code by hand. We run setup() to configure everything, fine. Then we run loop(). This calls the function receiveSMS(). Let's assume that there's nothing there at the moment - SIM900.available()==0 - so it drops through and returns to loop(). Then we have an infinite loop....

If there is something available, perhaps information returned from the previous commands, and the first character isn't a '#', then drop through and return to loop() - as above.

Perhaps what you want to do is if there is something to read, read it, and if '#' then process. If not, discard and wait for the next character to appear?