> For the complete documentation index, see [llms.txt](https://calnix.gitbook.io/eth-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calnix.gitbook.io/eth-dev/compendium/control-variable-visibility.md).

# Control Variable Visibility

Visibility modifiers **restrict** who can use values of Solidity variables. Here is a list of modifiers that change these permissions:

* `public`: anyone can get the value of a variable.
* `external`: only external functions can get the value of a local variable. It is not used on state variables.
* `internal`: only functions in this contract and inherited contracts can get values.
* `private`: access limited to functions from this contract.

{% hint style="warning" %}
**public = external  + internal**

public functions cost more gas than either external or internal. To save gas, restrict to external/internal when possible.
{% endhint %}

### public vs external

`public` and `external` differs in terms of gas usage. The former use more than the latter when used with large arrays of data. This is due to the fact that Solidity copies arguments to memory on a `public` function while `external` read from `calldata` which is cheaper than memory allocation.

* `public` functions are called **internally and externally**
* &#x20;`internal` calls are executed via jumps in the code because array arguments are passed internally by pointers to memory.&#x20;
* When the compiler generates the code for an internal function, that function expects its arguments to be located in memory.&#x20;
* That is why `public` functions are allocated to memory.&#x20;
* **The optimization that happens with `external` is that is does not care for the internal calls.**

{% hint style="info" %}
So if you know the function you create only allows for `external` calls, go for it. It provides performance benefits and you will save on gas.
{% endhint %}

## Immutable v Constant

* for constant variables, the value has to be fixed at compile-time, (initial and assign in one line)
* for immutable, it can still be assigned at construction time (within constructor)

```solidity
IERC20 public immutable collateral;    
IERC20 public immutable debt;  

constructor(address dai_, address weth_, address priceFeedAddress, uint bufferNumerator_, uint bufferDenominator_) {
    collateral = IERC20(weth_);
    debt = IERC20(dai_);
    priceFeed = AggregatorV3Interface(priceFeedAddress);
    // collateralization level
    bufferNumerator = bufferNumerator_;
    bufferDenominator = bufferDenominator_;
}
```


---

# 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 dynamically by asking a question.

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

```
GET https://calnix.gitbook.io/eth-dev/compendium/control-variable-visibility.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.
