Solidity Steps
  • Learning Solidity
  • Step 1
    • 1: Introduction
    • 2: Data Types
    • 3: Functions
    • 4: Control Structures
    • 5: State Variable
    • 6: Local Variables
    • 7: Global Variables
    • 8: View Keyword
    • 9: Pure Keyword
  • STEP 2
    • 10: Immunable Keyword
    • 11: Events
    • 12: Condition
    • 13: While Loop
    • 14: Do While Loop
    • 15: For Loop
    • 16: Required
    • 17: Assert
    • 18: Revert
    • 19: Modifier
  • STEP 3
    • 20: Constructor
    • 21: Mapping
    • 22: Array
    • 23: Enum
    • 24: Structs
    • 25: Data Location
    • 26: Inheritance
    • 27: The Shadowing Effect
    • 28: Super Keyword
    • 29: Visibility
  • STEP 4
    • 30: Interface
    • 31: Abstract Contract
    • 32: Payable
    • 33: Using type()
    • 34: Sending Ether
    • 35: Receive
    • 36: Fallback
    • 37: Call
    • 38: DelegateCall
    • 39: Calling Other Contracts
  • STEP 5
    • 40: Factory Contract
    • 41: Proxy Contract
    • 42: Create2
    • 43: Try and Catch
    • 44: Solidity Library
    • 45: ABI Encoded
    • 46: ABI Decoded
    • 47: Keccak256
    • 48: Function Signature Hash
  • TIPS
    • Tips: Solidity by "Immunable"
    • Tips: Truffle Tutorial
    • Tips: Microblog Dapp
    • Tips: Reentrancy
    • Tips: Slither Tutorial
    • Tips: Remix Tutorial
    • Tips: Hardhat Tutorial
  • CAREER
    • 💲Cover Letter
    • 💲Resume
  • ABOUT
    • Contact me
Powered by GitBook
On this page
  • Call Function
  • Call() Function
  1. STEP 4

37: Call

Call Function

A call function is a type of function in Solidity that sends a message from one contract to another while not creating a transaction on the blockchain. This is useful when you need to read data from a contract without making any changes, as it is faster and costs less gas.

  • Example 1 The following code shows an example of a call function which is used to read the value of a variable myVar from a contract called MyContract:

contract Caller {    
    function readMyVar() public view returns (uint) {        
        return MyContract.myVar();    
    }
}
  • Example 2 The following code shows an example of a call function which is used to call a function makePayment from a contract called PaymentContract:

contract Caller {    
    function makePayment(address _to, uint _amount) public {        
        PaymentContract.makePayment(_to, _amount);    
    }
}

Call() Function

The call() function is a powerful tool for Ethereum developers. It allows for sending transactions to other contracts without having to interact with them directly. This is extremely useful for testing, creating transactions that would otherwise be too expensive, or for interacting with contracts that you don't own.

  • Example 1 Let's say we have a contract called MyContract that sends a message to another contract when called. We can call MyContract.call() like this:

MyContract.call({  to: <address of other contract>,  data: <data to send>})
  • Example 2 We can also use call() to send Ether to a contract. Let's say we have a contract called MyFund that accepts Ether donations. We can call MyFund.call() like this:

MyFund.call({  to: <address of MyFund>,  value: <amount of Ether to send>})
  • Example 3 Finally, we can use call() to call other functions of a contract. Let's say we have a contract called MyToken that has a function called transfer() for transferring tokens. We can call MyToken.call() like this:

MyToken.call({  
    to: <address of MyToken>,  data: <data for transfer() function>
})

Using the call() function can be a powerful way to interact with contracts that you don't own, as well as for testing and creating transactions that would otherwise be too expensive.

That's it for the lesson 37! In the next lesson, Delegate Call

Previous36: FallbackNext38: DelegateCall

Last updated 1 year ago