function withdraw (uint amount) public returns(uint) {
require (balance[msg.sender]>=amount, "balance not sufficient");
msg.sender.transfer (amount);
balance[msg.sender] -= amount;
return balance[msg.sender]; // why do we sometimes return, and sometimes not return?
uint PsenderBalance = balance[msg.sender];
assert (balance [msg.sender]== PsenderBalance - amount );
}
//
-
If i change the name PsenderBalance to previousSenderBalance which was used in transfer function, will there be error since both function are separate functions?
-
Can i return balance[msg.sender] before the assert code ?
-
i am a bit confused because i see that sometimes function does not return value like
mapping (address=> User) users;
struct User{
uint id;
uint balance;
}
function updateBalance (uint id, uint balance) public {
User storage user= users[id];
}
// I am thinking maybe if the mapping is already declared at the top, then this would be storage data type automatically and hence do not need to be returned, so there is no need to return.
But in the withdraw function, the mapping with the name balance, is also declared, but it needs to be returned in withdraw function.
I am confused but keen to learn about it. Please advise me the way out of my doubts.
Thank you