DEV Community

Cover image for Getter and Setter functions in Solidity
Shlok Kumar
Shlok Kumar

Posted on

Getter and Setter functions in Solidity

In Solidity, getter and setter functions are used to read and write values from variables.

Getter and setter functions are used in Solidity to read and modify the values of variables stored in a smart contract. Getters are functions that return a value, while setters are functions that modify the value of a variable. Getters and setters must be declared with the correct visibility to work properly.

Getter and setter functions should be used instead of public variables, as public variables can be difficult to control. This is because getters and setters allow for more control over how data is accessed and modified, making it easier to maintain security within the contract. Additionally, using getters and setters can help reduce gas costs by avoiding unnecessary code deployment.

When creating getter and setter functions for mapped structs, it is necessary to write custom functions to access or modify indexed values. This is because there is no automatic way to create getter or setter functions for mapped structs in Solidity. However, there are well-solved storage patterns available that can help guide developers when writing their getter and setter functions for mapped structs.

In Solidity, getters and setters are declared using the keyword "contract". For example, to create a getter and setter for a score variable, one would declare it as follows:

contract Score { 
    uint score = 5; 
    function getScore() public returns (uint) { 
        return score; 
    } 
    function setScore(uint new_score) public { 
        score = new_score; 
    } 
}
Enter fullscreen mode Exit fullscreen mode

Getters and setters are important for keeping control over variables in smart contracts. Public variables should be avoided as they can be difficult to keep track of. By using getters and setters instead, one can ensure that only authorized users have access to certain data or functions within the contract.

You have to send a transaction if you want to write to or change a state variable. On the other hand, you don't have to pay anything to read state variables.

 A setter is a function in Solidity that changes the value of a variable (modifies the state of the contract). When you declare your function, you must tell it what parameters it will take.

In Solidity, a getter is a function that returns a value.

Example:

 // SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
    // State variable to store a number
    uint public num;

    // You need to send a transaction to write to a state variable.
    function set(uint num) public {
        num = num;
    }

    // You can read from a state variable without sending a transaction.
    function get() public view returns (uint) {
        return num;
    }
}
Enter fullscreen mode Exit fullscreen mode

 For more content, follow me at - https://linktr.ee/shlokkumar2303

Top comments (0)