# Functions

**EVM**

Functions that can be called are marked as `external` or `public`

```solidity
contract Dex {
  function swap(uint256 amountIn, address token) external payable {
    // Buy token
  }
  
  function swap2(uint256 amountIn, address token) public payable {
    // Buy token
  }
}

contract Token {
  mapping(address user => uint256 balance) public balances;
}
```

**Solana**

1. **The `program` macro specifies the module containing the program's instruction logic, it is equivalent to an EVM `contract`.**
2. An `instruction` is the equivalent of an EVM function.
3. Each instruction comes with a context. A context contains all the accounts an instruction needs to access. In Solana, data is stored in accounts, it is similar to an EVM mapping, but they are not the same because in EVM, data is stored within a contract while Solana programs are stateless. Data is stored separately and their storage addresses are derived.
4. In this example, we have to specify the buyer's token account because a successful swap increments the buyer's token account's balance (more on token accounts in another section). The `buyer_token_account` is the equivalent of the EVM Token's `balances` mapping.

```rust
#[derive(Accounts)]
pub struct Swap<'info> {
  pub token: Account<'info, Mint>,
  
  #[account(
    init_if_needed,
    payer = buyer,
    associated_token::mint = mint,
    associated_token::authority = buyer
  )]
  pub buyer_token_account: Account<'info, TokenAccount>,
  
  pub buyer: Signer<'info>
}

#[program]
pub mod dex {
  pub fn swap(Context<Swap>, amount_in: u64, token: Pubkey) {
    // Buy token
  }
}
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://0xkowloon.gitbook.io/anchor-for-evm-developers/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
