A value returned by a function in the smart contract cannot be captured as every function returns a hash.
Then, why write return statements in Solidity function?
For example,
event LogNewVoter(address sender, string name, bool isSuccess);
function addVoter(address _voter,string _name, bytes _voterImageHash) payable public returns(bool) {
require(msg.sender == votingAdmin);
Voter storage sender = voters[_voter];
if(sender.isVoter){
revert("This address is all ready registered as a voter!");
//throw;
}
voters[_voter].name = _name;
voters[_voter].hasVoted = false;
voters[_voter].isVoter = true;
voters[_voter].voterImageHash = _voterImageHash;
LogNewVoter(msg.sender,_name,true);
return true;
}
In above question, I can get whether the voter is registered successfully or not by using events. If I can not capture value returned by a function then why to write return statements in function?
Best Answer
In Solidity you can return values from functions to the user if these functions are declared view(/constant).
When they are not declared as view you can still get a return value by using .call() this will execute the call locally and not mine it onto the blockchain however.
Return values do play a big role as they can be returned to other functions even when the function is not view/constant. I'll give a real world example.
The following function creates a new 'bounty' on a bounty contract, it returns a bytes32 value.
The return value is then sent to this function on another contract. This will store the return value in an array so that the newly created bounty is associated with a user as well: