Search code examples
functionreturnethereumsoliditysmartcontracts

Is it necessary to write a return statement in Solidity?


pragma solidity ^0.8.0;

contract Counter {
    uint public number;
    string public name;

    constructor(string memory input_name, uint input_number){
        name = input_name;
        number = input_number;
    }

    function add () public {
        number ++;
    }

    function subtract() public {
        number --;
    }

    function update_name(string memory update) public {
        name = update;
    }

I wrote a very basic contract here and I'm wondering why I should write a return statement for each of my functions as it is already returns a value. Could someone explain my error or the point of the return statement here?


Solution

  • Solidity have the "implicit return" feature, so as you noticed, you may skip the "return" statement in some cases. However, it's discourages, since you're not explicit enough and may cause a lot of confusion and issues.

    For details please check the following links: