The evolution and function of cognition