There is no easy answer when it comes to finding the best price for travelling on a train in Spain. What most people do is consider all of their purchasing options prior to making a decision. You may purchase single tickets there and back, return tickets or rail passes. Try adding up the cost of single tickets there and back and noting the total, adding up the cost of return tickets and noting the total, finding cheapest travel pass and noting the total, and then comparing your totals to see which of those three options are the cheapest. You may find one option is cheaper for one location, and another option is cheaper for another location.
Sometimes it is cheaper to get singles there and singles back, and sometimes it isn’t. It may also be cheaper to take a less direct route to your destination. Strongly consider mixing up your journey with a train and buses, and do plenty of research into return journeys because they are typically (but not always) 20% cheaper than single tickets. There are sometimes travel discounts for advanced booking, so it may be worth doing your research sooner rather than leaving it until you arrive in Spain.
Spain doesn’t run a rail system that allows for an easy hop-on hop-off adventure. Many train journeys require that you book or reserve a seat prior to getting on the train. It will say on the train schedule if a seat booking is required. Even with a rail pass, you may still be required to book a seat on the train. Many seats may be booked 60 days in advance, so make sure you know exactly what time you wish to depart, and make sure you are at the station at the right time.
Trains probably shouldn’t be your first choice for transport if you are only planning to tour Barcelona. However, if you are planning to visit the rest of Spain, France, Portugal and/or Italy, the train may be just what you need. The service within Spain and between these countries is highly integrated and easy to use once you become accustom with how it works. There are many cheaper, easier and more frequent travel options if you are only visiting and/or touring Barcelona.