Function visibility / Access modifiers in solidity

Visibility Modifier
Description
Accessible from
public
Functions declared as public can be called from anywhere (within the contract, externally, and by other contracts).
Everywhere
internal
Functions declared as internal can only be called from within the contract and derived contracts.
Within the contract and derived contracts
private
Functions declared as private can only be called from within the contract where they are defined.
Only within the same contract
external
Functions declared as external can only be called from outside the contract (externally).
Only externally
public Function: Functions declared as public can be called from anywhere, including within the contract, from other contracts, and externally (by anyone). This is the default visibility for functions in Solidity
Let's say you have a simple voting contract where people can vote for a candidate.
Solidity
Copy
pragma solidity ^0.8.0; contract Voting { mapping(string => uint256) public votes; function voteForCandidate(string memory candidateName) public { votes[candidateName]++; } }
In this example, the voteForCandidate function is public, allowing anyone to call it and vote for a candidate.
2.Internal Function:
Functions declared as internal can only be called from within the contract and derived contracts. They are not accessible externally.
For example, let's say you have a contract that stores and updates data about a company's employees. You may have a function that updates an employee's bonus and you want to restrict access to this function to only the contract itself and any derived contracts.
Solidity
Copy
pragma solidity ^0.8.0; contract EmployeeData { mapping(uint256 => Employee) public employees; struct Employee { string name; uint256 bonus; } function updateEmployeeBonus(uint256 employeeId, uint256 newBonus) internal { employees[employeeId].bonus = newBonus; } }
In this example, the updateEmployeeBonus function is internal and can only be called from within the contract and any derived contracts.
3.private: Functions declared as private can only be called from within the contract where they are defined. They are not accessible even in derived contracts.
For example, let's say you have a contract that manages a user's bank account and you want to have a function that allows the user to withdraw funds from their account. You may want to restrict access to this function to only the contract itself and not allow it to be called from any derived contracts.
Solidity
Copy
pragma solidity ^0.8.0; contract BankAccount { mapping(address => uint256) private balances; function withdraw(uint256 amount) private { balances[msg.sender] -= amount; } }
In this example, the withdraw function is private and can only be called from within the same contract. It is not accessible even in derived contracts.
Example 2 : In a decentralized application (DApp) with user profiles, you might have a private function to update a user's email address.
Solidity
Copy
pragma solidity ^0.8.0; contract UserProfile { mapping(address => string) private userEmails; function setUserEmail(string memory newEmail) public { userEmails[msg.sender] = newEmail; } function getUserEmail() public view returns (string memory) { return userEmails[msg.sender]; } }
The setUserEmail function is public to allow users to set their email addresses, but the userEmails mapping is kept private to restrict access to email addresses.
External Function: Functions declared as external can only be called from outside the contract (externally). This means they cannot be called from within the contract or by other contracts.
For example, let's say you have a contract that stores and verifies a user's identity. You may want to have a function that allows external entities (such as a government agency) to verify the user's identity.
Solidity
Copy
pragma solidity ^0.8.0; contract IdentityVerification { mapping(address => bool) private verifiedUsers; function verifyIdentity(address user) external { verifiedUsers[user] = true; } }
In this example, the verifyIdentity function is external and can only be called from outside the contract. It is not accessible from within the contract or by other contracts.