Visibility modifiers (public, private, internal, external) in Blockchain / Solidity - Time & Space Complexity
When working with blockchain smart contracts, functions have visibility modifiers that control who can call them.
We want to understand how these modifiers affect the number of operations when functions are called.
Analyze the time complexity of calling functions with different visibility modifiers.
contract Example {
uint data;
function setDataPublic(uint x) public {
data = x;
}
function setDataPrivate(uint x) private {
data = x;
}
function callPrivate(uint x) public {
setDataPrivate(x);
}
}
This contract has functions with public and private visibility and shows calling a private function internally.
Look for repeated function calls or loops that affect execution time.
- Primary operation: Calling functions with different visibility.
- How many times: Each function call happens once per external or internal call.
The number of operations grows directly with how many times functions are called.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 calls | 10 function executions |
| 100 calls | 100 function executions |
| 1000 calls | 1000 function executions |
Pattern observation: Each call adds a fixed amount of work, so total work grows linearly with calls.
Time Complexity: O(n)
This means the total work grows in direct proportion to the number of function calls made.
[X] Wrong: "Private functions run faster or cost less time than public ones because they are hidden."
[OK] Correct: Visibility controls who can call the function, but the time to execute the function itself is about what the code does, not its visibility.
Understanding how visibility affects function calls helps you reason about contract design and performance in real blockchain projects.
"What if a public function calls an external function multiple times inside a loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand visibility modifiers
Public functions can be accessed anywhere, inside or outside the contract.Step 2: Compare with other modifiers
Private is only inside the contract, internal is inside and inherited contracts, external only callable from outside.Final Answer:
public -> Option AQuick Check:
public = open access [OK]
- Confusing private with public access
- Thinking internal allows outside calls
- Assuming external works inside contract
Solution
Step 1: Identify external function syntax
External functions are declared with the keywordexternalafter the function name.Step 2: Check other options
Private and internal restrict access inside the contract or inheritance, public allows all calls.Final Answer:
function foo() external {} -> Option BQuick Check:
external = outside calls only [OK]
- Using private instead of external
- Confusing public with external
- Missing parentheses or braces
contract Test {
uint private data = 10;
function getData() public view returns (uint) {
return data;
}
function setData(uint _data) external {
data = _data;
}
}What will happen if another contract tries to call
data directly?Solution
Step 1: Check variable visibility
The variabledatais declaredprivate, so it cannot be accessed outside the contract.Step 2: Understand access from other contracts
Other contracts cannot access private variables directly, only public or external functions.Final Answer:
It will fail because data is private -> Option CQuick Check:
private = no outside access [OK]
- Assuming private variables are public by default
- Confusing external with private
- Thinking public functions expose private variables directly
contract Sample {
uint internal count = 0;
function increment() private {
count += 1;
}
function callIncrement() public {
increment();
}
}Which of the following fixes the error preventing
increment() from being called?Solution
Step 1: Check function call context
The functionincrement()is private and called inside the same contract bycallIncrement().Step 2: Understand private function access
Private functions can be called inside the same contract, so this call is valid.Final Answer:
No fix needed, code is correct -> Option AQuick Check:
private functions callable inside contract [OK]
- Thinking private functions can't be called inside contract
- Confusing internal with private
- Changing visibility unnecessarily
Solution
Step 1: Understand access needs
The function should be callable inside this contract and by derived contracts, but not externally.Step 2: Match visibility modifier
Internal visibility allows access inside the contract and by inheriting contracts only.Final Answer:
internal -> Option DQuick Check:
internal = contract + inheritance only [OK]
- Choosing public which allows external calls
- Choosing private which blocks inheritance access
- Confusing external with internal
Ready to prove it?
Pass the test and this pattern moves unknown
