Earn While Learning Web3 Programming>> Start Here FREE!!!
EXERCISE:
Try for "Private" Functions:
i. See the the Smart Contract Code used in this course video in the forum answer below
ii. Try change The "public" in Balance Mapping to "private". Deploy the Smart contract and study the changes
iii. Try change The "public" in totalDeposited to "private". Deploy the Smart contract and study the changes
iv. Summarize your observations below
v. Post your code in this forum:
NOTE:
This Is An Exercise From Our FREE Smart Contract Development With Solidity For Beginners Course (If you have not enrolled yet, you can join the course totally for FREE).
Functions in solidity by Payment/Fund Transfer capability allows you to program your smart contract to send out fund to a wallet(s).
1. Public State Variable Function
For Example When you add "Public" to a state variable it auto create View/Getter function to access the variable data value.
Examples use case: check source code below
Public State Variable Function is written in solidity as:
uint public totalDeposited; //create a View/Getter Function by automatically to check the value/balance of totalDeposited
uint totalDeposited; //without the public added, the totalDeposit balance will be hidden but still accessible for any one or program to call and executed to check the value within and outside your smart contract.
2. Private State Variable Function
For Example When you add "Private" to a state variable it auto create View/Getter function to access the variable data value but limit its call and accessibility to only functions within the same Smart contract and prevent outsider from calling the variable to check the variable value or content it holds..
Examples use case: check source code below
Private State Variable Function is written in solidity as:
uint private totalDeposited; //create a View/Getter Function by automatically to check the value/balance of totalDeposited only by the functions within the smart contract itself.
uint totalDeposited; //without the public/private added, the totalDeposit balance will be hidden but still accessible for any one or program to call and executed to check the value within and outside your smart contract.
Want to know how to make your smart contract receive funds in solidity? Then checkout this Payable Functions in Solidity
Example PUBLIC VARIABLE Function Source Code Used in THE FREE Course VIDEO ABOVE You Should Copy and Use To Do the above Exercise
pragma solidity 0.8.7;
contract VariableFunction {
/*
SC SUMMARY: To learn about Functions By Variable Accessibility in solidity.
*///mapping balance
mapping (address => uint) public Balance;//track SC all users total Deposited Balance
uint public totalDeposited;//PAYABLE FUNCTION - Deposited
function depositEther() public payable {//track deposit to mapping
Balance[msg.sender] += msg.value;//add to total SC deposited Balance
totalDeposited += msg.value;}
//Check Balances
function etherBalance() public view returns(uint, uint) {return (Balance[msg.sender], totalDeposited);
}
/*
//PAYABLE FUNCTION - Transfer/Withdraw
function withdrawEther(uint _ethAmount) public {//SC Transfers to user wallet
msg.sender.transfer(_ethAmount);//withdrawEther
Balance[msg.sender] -= _ethAmount;//totaDepositWithdraw
totalDeposited -= _ethAmount;}
*/}