In Ethereum smart contract, address and function are broadly used value types. In this article, we'll see the usage of both. If you’re new to here, I would encourage you to read the previous article of Variables and Types in Solidity.
Function Type
Solidity functions are methods used to perform some specific task. They get invoked during the execution of a smart contract. We've already used functions in previous articles; in our first smart contract of HelloWorld, printHelloWorld was a function.
- pragma solidity ^ 0.5 .0;
- contract helloWorld {
- function printHelloWorld() public pure returns(string memory) {
- return 'helloWorld';
- }
- }
Function Structure
- function (<parameter types>) {internal|external|public|private} [pure|view|payable] [returns (<return types>)]
Function Parameters
Parameter types are the input variables. It can be used as any other local variables with an assignment. In the following example, _a and _b are function parameters.
- pragma solidity ^0.5.0;
- contract Types {
- uint sum;
- function result(uint _a, uint _b) public {
- sum = _a + _b;
- }
- }
Return Variables
Return variables are used to return something from the function. We can pass return variables with "returns" keyword. Let's understand it with an example. In the above function, we've stored the sum of _a and _b to the state variable named "sum". Now, I want to return state variable value and for that, we have to modify the result function as below.
- pragma solidity ^0.5.0;
- contract Types {
- uint sum;
- function result(uint _a, uint _b) public returns(uint){
- sum = _a + _b;
- return sum;
- }
- }
- pragma solidity ^ 0.5 .0;
- contract Types {
- function result(uint _a, uint _b) public pure returns(uint sum) {
- sum = _a + _b;
- }
- }
In Solidity function, you can also return two or more values.
- pragma solidity ^ 0.5 .0;
- contract Types {
- uint sum;
- uint mul;
- function result(uint _a, uint _b) public returns(uint, uint) {
- sum = _a + _b;
- mul = _a * _b;
- return (sum, mul);
- }
- }
View Function
Functions which will not alter the storage state can be marked as a view function. In simple terms, it is used for viewing the state.
- pragma solidity ^0.5.0;
- contract Types {
- function result(uint a, uint b) public view returns (uint) {
- return a + b + now; //"now" is current block timestamp in
- //seconds in unix epoch format
- }
- }
Pure Function
A function that does not modify or read the state, is called a pure function.
- pragma solidity ^0.5.0;
- contract Types {
- function result(uint a, uint b) public pure returns (uint) {
- return a * (b + 42);
- }
- }
Payable Function
Payable Functions allows to receive Ethers while it being executed, means that, if someone sends some Ethers to the smart contract, and it doesn't have a payable function, then smart contract won't accept ether and transaction will get failed. To catch that transfer amount, the smart contract needs to have a payable function.
For example, in below code, the receiveEther function is not payable, so when you deploy this contract and run the method receiveEther, it will give an error.
- pragma solidity ^ 0.5 .0;
- contract Types {
- function receiveEther() public {}
- }







Sarathlal SaseendranPosted Jan 25, 2019, 12:32 AM
Very good Divyang Desai. Actually your articles on Solidity and Blockchain attract me to learn more on this new topic. Thanks a lot