DEFINATIONWhen you call a setter function it creates a transaction that needs to be mined and costs gas because it changes the blockchain.
When you declare a public state variable a getter function is automatically created.
By default variable visibility is private.
Solidity - View and Pure Functions - GeeksforGeeks
1️⃣ Security: By explicitly declaring functions as view or pure, you provide clarity to other developers and auditors regarding the intended behavior of the function. This helps prevent unintentional modifications to the state and minimizes the risk of introducing vulnerabilities.
2️⃣ Gas Efficiency: View and pure functions do not require transactions to be executed on the blockchain. As a result, they do not consume any gas, making them more cost-effective when retrieving data or performing computations.
3️⃣ Code Reusability: View and pure functions can be called by other functions within the same contract or by external contracts. This promotes code reusability and modular design, as these functions can be used in multiple contexts without concerns about state modifications.
4️⃣ Optimization: Since pure functions are entirely self-contained and do not rely on external variables, they can be optimized by the compiler more aggressively. This can lead to improved performance and reduced gas costs.