1

I am a bit confused about the uint "id" in the start function.

I understand that the uint "nextCampaignId" is a global variable, and so changes made to it will be saved in storage; which means that with each new campaign created, "nextCampaignId" will be incremented, and so cannot be used to determine ID numbers of previously created campaigns. Hence, we need to assign its value to a new uint "id" within the function.

However, I thought integers declared within functions do not get saved in storage. Wouldn't this ID number be lost once the function is terminated? Does the function simply return the ID (to the UI) and then forgets it? But then how can we retrieve specific campaign data later?

uint nextCampaignId;
mapping(uint256 => CampaignData) campaigns;

// Start a new campaign.
function start(address recipient, uint256 goal, uint256 deadline) returns 
(uint id) {
    var campaign = campaigns[nextCampaignId];
    campaign.recipient = recipient;
    campaign.goal = goal;
    campaign.deadline = deadline;
    nextCampaignId ++;
    id = nextCampaignId;
}
1

Almost everything you say is true, but nextCampaignId isn't declared inside the function. It's just used inside the function.

If it was declared there, the function would look something like this:

function start(address recipient, uint256 goal, uint256 deadline) returns 
(uint id) {
    var campaign = campaigns[nextCampaignId];
    campaign.recipient = recipient;
    campaign.goal = goal;
    campaign.deadline = deadline;
    uint nextCampaignId;
    nextCampaignId ++;
    id = nextCampaignId;
}

Also, the reason why id = nextCampaignId; is needed is just because the function header says that the variable id should be returned. Nothing else. It could also be a simple return nextCampaignId;.

  • The directl answer to your question is the last part. id = xxxx is simply setting the return value. It could have been written as return nextCampignId; – Thomas Jay Rush May 27 '18 at 3:34
  • I know "nextCampaignId" is declared outside of the function (I was referring to "uint ID" within the funtion). But then it will be changed with each new campaign. So, for example, the second campaign will change this to 2, but then wouldn't the ID "1" for the first campaign be lost? – CreatedAMadman May 27 '18 at 4:30
  • What do you mean by "lost"? Whoever calls start() can simply save the id if he's going to need it again. In the contract it's implicitly stored since it's the index in the campaigns array. – mafrasi2 May 27 '18 at 4:33
  • I would think that the function would increment the global variable nextCampaignId each time it is used and so returning this would only bring back the latest number. – CreatedAMadman May 27 '18 at 4:36
  • Yes, and the latest number is the id of the new campaign, which is exactly what you would expect the function start() to return. When you (as a user) call start() you get back the id of the campaign you created. You can then store that somewhere or just remember it. When you want to do something to the campaign, you send that id to the contract and the contract can then lookup the id in the campaigns array to retrieve your campaign. – mafrasi2 May 27 '18 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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