Functions

Functions in Solidity are modules of code that are associated with a contract. Functions are declared with a name, optional parameters, access modifier, optional constant keyword, and optional return type. This is shown in the following example:

function orderMatcher (uint x)  
private constant returns(bool return value)  

In the preceding example, function is the keyword used to declare the function. orderMatcher is the function name, uint x is an optional parameter, private is the access modifier or specifier that controls access to the function from external contracts, constant is an optional keyword used to specify that this function does not change anything in the contract but is used only to retrieve values from the contract and returns (bool return value) is the optional return type of the function.

  • How to define a function: The syntax of defining a function is shown as follows:
      function <name of the function>(<parameters>) <visibility specifier> returns 
(<return data type> <name of the variable>) { <function body> }
  • Function signature: Functions in Solidity are identified by its signature, which is the first four bytes of the Keccak-256 hash of its full signature string. This is also visible in Remix IDE, as shown in the following screenshot. f9d55e21 is the first four bytes of 32-byte Keccak-256 hash of the function named Matcher.
Function hash as shown in Remix IDE

In this example function, Matcher has the signature hash of d99c89cb. This information is useful in order to build interfaces.

  • Input parameters of a function: Input parameters of a function are declared in the form of <data type> <parameter name>. This example clarifies the concept where uint x and uint y are input parameters of the checkValues function:
      contract myContract 
      { 
          function checkValues(uint x, uint y) 
          { 
          } 
      } 
  • Output parameters of a function: Output parameters of a function are declared in the form of <data type> <parameter name>. This example shows a simple function returning a uint value:
      contract myContract 
      { 
          function getValue() returns (uint z) 
          { 
              z=x+y; 
          } 
      } 

A function can return multiple values. In the preceding example function, getValue only returns one value, but a function can return up to 14 values of different data types. The names of the unused return parameters can be omitted optionally.

  • Internal function calls: Functions within the context of the current contract can be called internally in a direct manner. These calls are made to call the functions that exist within the same contract. These calls result in simple JUMP calls at the EVM bytecode level.
  • External function calls: External function calls are made via message calls from a contract to another contract. In this case, all function parameters are copied to the memory. If a call to an internal function is made using the this keyword, it is also considered an external call. The this variable is a pointer that refers to the current contract. It is explicitly convertible to an address and all members for a contract are inherited from the address.
  • Fallback functions: This is an unnamed function in a contract with no arguments and return data. This function executes every time Ether is received. It is required to be implemented within a contract if the contract is intended to receive Ether; otherwise, an exception will be thrown and Ether will be returned. This function also executes if no other function signatures match in the contract. If the contract is expected to receive Ether, then the fallback function should be declared with the payable modifier. The payable is required; otherwise, this function will not be able to receive any Ether. This function can be called using the address.call() method as, for example, in the following:
      function () 
      { 
          throw; 
      } 

In this case, if the fallback function is called according to the conditions described earlier; it will call throw, which will roll back the state to what it was before making the call. It can also be some other construct than throw; for example, it can log an event that can be used as an alert to feed back the outcome of the call to the calling application.

  • Modifier functions: These functions are used to change the behavior of a function and can be called before other functions. Usually, they are used to check some conditions or verification before executing the function. _ (underscore) is used in the modifier functions that will be replaced with the actual body of the function when the modifier is called. Basically, it symbolizes the function that needs to be guarded. This concept is similar to guard functions in other languages.
  • Constructor function: This is an optional function that has the same name as the contract and is executed once a contract is created. Constructor functions cannot be called later on by users, and there is only one constructor allowed in a contract. This implies that no overloading functionality is available.
  • Function visibility specifiers (access modifiers): Functions can be defined with four access specifiers as follows:
    • External: These functions are accessible from other contracts and transactions. They cannot be called internally unless the this keyword is used.
    • Public: By default, functions are public. They can be called either internally or using messages.
    • Internal: Internal functions are visible to other derived contracts from the parent contract.
    • Private: Private functions are only visible to the same contract they are declared in.
  • Function Modifiers:
    • pure: This modifier prohibits access or modification to state
    • view: This modifier disables any modification to state
    • payable: This modifier allows payment of ether with a call
    • constant: This modifier disallows access or modification to state
  • Other important keywords/functions throw: throw is used to stop execution. As a result, all state changes are reverted. In this case, no gas is returned to the transaction originator because all the remaining gas is consumed.
..................Content has been hidden....................

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