> For the complete documentation index, see [llms.txt](https://solstep.gitbook.io/solidity-steps/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://solstep.gitbook.io/solidity-steps/step-4/39-calling-other-contracts.md).

# 39: Calling Other Contracts

### Calling Other Contracts

Calling other contracts is an important part of writing Solidity code. It enables you to interact with smart contracts on the Ethereum blockchain from other contracts. In this article, we'll explore a few examples of contract-to-contract interaction and the various ways of calling other contracts.

* **Example 1**: Calling a Function from Another Contract The most basic way to call a function from another contract is to use the delegatecall keyword. This allows you to call a function from another contract and have the code execute in the context of the calling contract.

Let's look at a simple example. We have two contracts, ContractA and ContractB. ContractA wants to call a function in ContractB:

```solidity
contract ContractA {    
    ContractB b; 
    constructor(ContractB _b) public {    b = _b;    } 
    function callContractB() public {        
        b.delegatecall(bytes4(keccak256("foo()")));    
    } 
} 
contract ContractB {    
    function foo() public {        // Do something    }
}
```

In this example, ContractA calls a function in ContractB using the delegatecall keyword. The delegatecall keyword takes a single argument, which is the function signature of the function to be called. In this example, we are calling the foo() function, so we pass in the function signature as an argument.

* **Example 2**: Calling a Function from Another Contract with a Return Value In the previous example, we saw how to call a function from another contract without a return value. But sometimes you may want to call a function from another contract and get a return value. To do this, we can use the call keyword.

Let's look at a simple example. We have two contracts, ContractA and ContractB. ContractA wants to call a function in ContractB and get a return value:

```solidity
contract ContractA {    
    ContractB b; 
    constructor(ContractB _b) public {        b = _b;    } 
    function callContractB() public {        
        (bool success, uint256 result) = b.call(
            bytes4(keccak256("foo()"))
        );    
    } 
} 
contract ContractB {    
    function foo() public returns (uint256) {        return 42;    }
}
```

In this example, ContractA calls the foo() function in ContractB using the call keyword. The call keyword takes two arguments, the function signature and a return value. In this example, we are calling the foo() function, so we pass in the function signature as the first argument and the return value as the second argument.

### Conclusion

In this article, we looked at a few examples of contract-to-contract interaction and the various ways of calling other contracts. We saw how to call a function from another contract using the delegatecall keyword and how to call a function from another contract and get a return value using the call keyword.

*That's it for the lesson 39! In the next lesson, Factory Contract*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://solstep.gitbook.io/solidity-steps/step-4/39-calling-other-contracts.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
