Language is to communicate. If I write this, I want someone to understand it. If I speak to myself, I express my own thoughts to myself, with the intention that I will understand it, at least.
Expression of one's thoughts is also communicating - against what y'man across says.
We wouldn't have language if it wasn't to communicate. To speak is to express an idea for the consumption of an intended audience (and sometimes an unintended audience).
If someone speaks a different language to you and they try to talk to you - or write something down for you to read - and you can't speak/read that language, it doesn't matter. What matters is that they *intended* for you to understand them. Whether you understand it or not is neither here nor there.
The primary function of language is expressing thoughts. When you think in your head, you often think in one language or another. I would know; I think in either Cantonese or English most of the time, and occasionally in Putonghua or French. When these thought processes happen, you are not using language to communicate.
Another purpose, although far less important, is as art. Language is often used as art. Chinese characters in particular have beautiful forms that will appeal even to foreigners who have no knowledge of the language because of their aesthetic beauty. Moreover, certain rhetorical devices like rhyming make poetry and other forms of literature sound better without really contributing to communication.