0

This is my first experience with programming on Ethereum. I'm deploying the sample contract following the instructions shown here.

// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.7.6;

contract testContract {

    uint256 value;

    constructor (uint256 _p) {
        value = _p;
    }

    function setP(uint256 _n) payable public {
        value = _n;
    }

    function setNP(uint256 _n) public {
        value = _n;
    }

    function get () view public returns (uint256) {
        return value;
    }
}

I don't understand how the variable _p works here. It shows up as an argument for the constructor. But I did not set it at any point before deployment and do not know how to set it after deployment. I also see that when I call the get function, it returns value which takes on _p due to the constructor.

I have deployed this contract on Ropsten here: https://ropsten.etherscan.io/address/0xfbad55ee853c127faed5ed30f367e43418226e87.

So what is the uint _p and where along the process was it set?

1 Answer 1

1

The deployed constructor, which is executed at deployment time, sets value=_p

4
  • Sorry, how is _p set in the first place? Commented Aug 9, 2021 at 8:02
  • 1
    When the contract is deployed, the code in the constructor is executed, which is the mechanism of smart contracts. This means that you must construct parameters (if any) during deployment, otherwise the deployment will fail.
    – zmy
    Commented Aug 9, 2021 at 8:27
  • Ah I see - the deployment in Remix was just a click so I guess it did it for me Commented Aug 9, 2021 at 8:42
  • Yes.不不不不不不不不
    – zmy
    Commented Aug 9, 2021 at 8:46

Your Answer

By clicking Post Your Answer, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.