How to do it...

  1. Create a function in solidity with the function keyword to perform a specific task. The following example demonstrates how we can create a simple function:
contract Test {
function functionName() {
// Do something
}
}
  1. Create another function to accept input parameters. These parameters can be declared just like variables. The following example accepts two parameters of type uint:

contract Test {
function add(uint _a, uint _b) public {
// Do something
}
}
  1. Define output parameters with the same syntax. The returns keyword is used to specify the return. The following function returns a variable of type uint:
contract Test {
function add(uint _a, uint _b) public pure
returns (uint sum)
{
sum = _a + _b;
}
}
  1. Declare return types without a name. They will use the same name as the variable returned:
contract Test {
function add(uint _a, uint _b) public pure
returns (uint)
{
uint sum = _a + _b;
return sum;
}
}
  1. Create a function with multiple output parameters. You can see a function returning two uint values in the following example:
contract Test {
function arithmetics(uint _a, uint _b) public pure
returns (uint, uint)
{
return (_a + _b, _a - _b);
}
}
  1. Try calling the functions of the current contract internally by its name. The example described here shows a function, addTwo, calling another function, addOne, which is declared within the same contract:
contract Test {
function addOne(uint _a) public pure
returns (uint)
{
return _a + 1;
}

function addTwo(uint _b) public pure
returns (uint)
{
return addOne(_b) + 1;
}
}
  1. Try calling functions declared within other contracts externally with this .f() or <contractInstance>.f(). In this example, the addOne and addTwo functions are declared within different contracts but called externally using the contract instance:
contract Basic {
function addOne(uint _a) public pure
returns (uint)
{
return _a + 1;
}
}

contract Advanced {
Basic basic;
function addTwo(uint _b) public view
returns (uint)
{
return basic.addOne(_b) + 1;
}
}

Calls to external contracts can cause several unexpected security issues. Untrusted contract calls may execute malicious code in that contract or any other contract that it depends upon. It is recommended to strictly validate such calls.

  1. Create a function that accepts Ether and has to be marked with payable. Non-payable functions will throw if someone tries to send Ether to it:
contract test {

mapping(address => uint) donors;

function donate() public payable {
donors[msg.sender] = msg.value;
}
}
  1. In a function call, argument names can be specified by name. This allows the user to follow a different order for passing parameters:
contract Test {
function add(uint _a, uint _b) public {
// Do something
}

function calc() public {
add({_b: 5, _a:10});
}
}
  1. Unused parameter names can be omitted from the function:
contract test {
function calc(uint _a, uint) public returns (uint) {
// Do something
}
}
  1. Functions can be declared as view (an alias for constant, which is deprecated) if they do not modify the state. State modification includes tasks such as writing to a state variable, emitting events, and sending Ether:
contract test {

uint multiplier = 10;

function fun(uint _a, uint _b) public view
returns (uint) {
return (_a + _b) * multiplier;
}
}
  1. Functions can be declared as pure if they do not read or modify the state. The following example does not read from or write to the contract state:
contract test {
function fun(uint _a, uint _b) public pure
returns (uint) {
return _a + _b;
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.255