U.S. stocks shook off early declines and closed out the last trading day of the month with modest gains on Friday as a rise in Microsoft helped offset declines in Amazon and Apple after disappointing quarterly earnings from the online retailer and iPhone maker.