Loading...
In this chapter, we will discuss how to return the multiple values in your contract
If you want to return any value then firstly, you will declare the return value in function
example:-
function example() public view returns(uint){}
In this previous example, we will return the unit value.
Let's take a serious example:-
pragma solidity ^0.4.24;
contract Arrays {
address owner;
uint supply;
mapping( address=>uint ) balances;
constructor(uint _supply) public{
supply = _supply;
owner = msg.sender;
balances[msg.sender] = supply;
}
function returnMultiple(address anyAddress) public view returns(uint,address,address,uint,uint){
return(balances[anyAddress],msg.sender,address(this),address(this).balance,(msg.sender.balance));
}
}
it's not a hard smart contract it's too easy. In this chapter, we motive to return the multiple things.
In this example, they have one function name "returnMultiple" in this function they have one parameter anyAddress then it's visibility than the return type. In this return type, you define those data types in which those you want to return means you want to output in which data type.
In this example, they will return the multiple data types-
returns(uint,address,address,uint,uint)
In this return statement, they have different values.
And Inside the function, you must ensure to define the return statement,
return(balances[anyAddress],msg.sender,address(this),address(this).balance,(msg.sender.balance));
Note:- if you want to declare the data type then you will use a returns statement. Otherwise, if you declare the actual value, they will return the same data type value as those values you already defined use return.
In this example, we will return the different values. they have first balances[anyAddress] they will return an integer value that's why first we define the return type unit and it will see our current balance supply. Then, msg.sender means who one deploy the contract, address(this) see our current contract address, address(this).balance see our current contract balance value that value is unit means integer, msg.sender.balance see our current address balance.