I know when to use
pure, but I dont know why we use it. Does EVM optimize anything When I use them?
For the same reason that you need to declare access-level (
private) in any object-oriented language.
It is not mandatory, and your code will compile successfully if you use
But by using a lower access-level, you are "declaring your intentions" - to yourself, as well as to anyone else using your code (or API).
So, for example, your clients (users) will know to "ignore" any function or variable declared
In addition, if you ever change a specific function (or access a specific variable) in a manner which is not permitted by its access-level, then the compiler will shout at you and remind you that it wasn't your original intention to begin with (hence you should either rethink your change or redeclare your intention).
I can't speak for any EVM level optimizations, but in high-security oriented languages like Solidity, this is often done to mark a method as without-a-doubt non state changing, which makes auditing much easier and clearer to follow.