Solidity Basics Getting Started (iii) strings and functions

Source: Internet
Author: User
Tags types of functions

string : You need to use double quotes "" or single quotation marks, for example: Define a String variable: string Name= "Jake";

String string cannot be obtained by length method

Hex Data : A string preceded by the keyword hex followed by a single or double quotation mark. such as Hex "001122FF". Here's an example to understand what this means:

Contract hexliteral{
    function test () returns (string) {
      var a = hex "001122FF";

      var B = hex "A";
      Expected primary expression
      
      return a;
  }

Since a byte is 8 bits, a hex is made up of two [0-9a-z] characters. so var B = hex "A"; a string that is not double is an error. The Var type is explained in a later article.

Hexadecimal literals can perform the same similar operations as strings (such as indexes):

pragma solidity ^0.4.0;

Contract hexliteralbytes{
    function test () returns (Bytes4, Bytes1, Bytes1, Bytes1, bytes1) {
      bytes4 a = hex "001122 FF ";

      Return (A, a[0], a[1], a[2], a[3]);
  }
}

Can be found, it can be implicitly converted to bytes, the above code execution results are as follows:

Result: " 0x001122ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000001100000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000 0000000000000000ff00000000000000000000000000000000000000000000000000000000000000 "
Transaction cost:21857 Gas . 
Execution cost:585 Gas.
Decoded: 
bytes4:0x001122ff
bytes1:0x00
bytes1:0x11
bytes1:0x22
bytes1:0xff

Functions: There are two types of functions, intrinsic functions (internal) and external functions (external), intrinsic functions can only be used within the context of the current contract, and external functions are allowed to be used outside of this contract. The default type of the function is an intrinsic function, and if the external function is called by an environment other than this contract, it is automatically converted to the Bytes24 type, which includes the 20-byte function address and the 4-byte function method signature.

Example: function ABC (UINT a) returns (UINT) {

return A;

}

ABC is the function name, inside the parentheses is the argument, if there are no parameters, the parentheses are empty, returns is the return value, you need to define the type of the return value within the parentheses, and if there is no return value, you must omit the Returns keyword.

Use an example to learn more about the changes:

Contract funtiontest{
    function Internalfunc () internal{}

    function Externalfunc () external{}

    function Callfunc () {
        //Call Internalfunc () directly using the internal method
        ;

        An external function cannot be called internally, and a compilation error is reported.
        //externalfunc ();
        error:undeclared identifier.
        Cannot call a ' internal '
        //member "internalfunc" not found or not visible in ' external ' mode after Argument-dependent lookup In contract funtiontest
        //this.internalfunc ();

        Use ' This ' to invoke an external function this.externalfunc () in ' external ' mode
        ;
    }
}
Contract functiontest1{function
    externalcall (funtiontest ft) {
        //call another contract's external function
        Ft.externalfunc ();
        
        Cannot invoke the intrinsic function of another contract
        //error:member "Internalfunc" not found or not visible after Argument-dependent lookup in contract Funtiontest
        //ft.internalfunc ();
    }
}

Explanation: The function internalfunc is defined as an intrinsic function that can be called directly. function externalfunc is defined as an external function, the direct call in this contract will be error, need to add the keyword This:this.externalFunc (). At the same time, adding the This function before the inner function will cause an error and cannot invoke the intrinsic function with the calling method of the external function.

Call the external function format for another contract: Ft.externalfunc (), FX is the name of another contract, Externalfunc is the name of the external function. The internal function of another contract cannot be called and will be an error.










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.