My Web3 Journey (Functions, Variables and Accessibility) part 2

jamiescript

Jamiebones

Posted on December 10, 2021

My Web3 Journey (Functions, Variables and Accessibility) part 2

Variables

There are three(3) types of variables in solidity. There are:
local variables

These are variables that are declared inside of a function.
There are not stored on the blockchain.

state variables
State variables are variables that are stored on the blockchain. It cost gas to store a state variable.

global variables
Global variables give information about the block chain.

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
  uint public storedNumber = 65757; //state variable
  //stored on the blockchain
  contract VariablesContract {
    function helloVariables() public {
    uint number = 567; 
    //local variable not stored on //the blockchain

  //global variable
  address client =  msg.sender; //the address of the person interacting with //the contract   
 }
  }

Enter fullscreen mode Exit fullscreen mode


`

Functions

Functions are methods used for changing data on the blockchain or returning data.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
  contract FunctionContract {
    uint public number = 677;
    function getNumber() public view returns (uint) {
        return number;
    }
  }
Enter fullscreen mode Exit fullscreen mode


`
This is a simple function called getNumbers we defined to retrieve the number variable. But before we get further into functions and explain what the term public and view means that is in the function definition; we need to talk about the accessibility of variables and functions.

Acessibility Modifiers

The base modifiers in solidity are :

  • external
  • public
  • private
  • internal The term accessibility can be liken to scope in Javascript. ( variable visibility) Let me explain what the terms above mean;

external

Only functions can be mark as external. This means that a function marked as external can only be called from another contract and not from the contract it was defined in.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

   contract SantaShoppingList {
    string[] public giftGivenArray;
     function rewardGoodfolks() public returns (string[] memory){
         WishList wish = new WishList();
         string[] memory giftItems = wish.myListForChristmas();
         giftGivenArray.push(giftItems[1]);
     }

   }
   contract WishList {
     function myListForChristmas() external returns (string[] memory) {
     string[] memory items = new string[](3);
     items[0] = ("play station 5");
     items[1] = ("mac book pro");
     items[2]=("bottle of wine");
     return items;
   }
}
}

Enter fullscreen mode Exit fullscreen mode


`
The code above show that we have two contracts defined which are SantaShoppingList and WishList. Inside the contract WishList we have a function which is called

myListForChristmas. This function is mark as external as I want an external contract created by Santa to be able to fulfil my wish list.

Don't worry too much about the code in this example that you do understand. You can run and deploy the above contract on Remix.

If you create another method inside the WishList contract to call the external marked function myListforChristmas you will find out that Remix will not compile your contract.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;


 contract WishList {

     function myListForChristmas() external returns (string[] memory) {
     string[] memory items = new string[](3);
     items[0] = ("play station 5");
     items[1] = ("mac book pro");
     items[2]=("bottle of wine");
     return items;
   }


  function getMyItemsfromSanta() public returns (string[] memory ) {
    string[] memory items = myListForChristmas();
    return items;
//this does not work because we defined myListForChristmas as an external function.
  }
}
Enter fullscreen mode Exit fullscreen mode


`
Key take away from the above that a function mark as external as the name implied can only be called from another contract. An advantage of declaring a function as external is that it helps to save gas.

internal

This is the default visibility for state variables (variables stored on the blockchain ). Internal functions and state variables can both be accessed from within the same contract and deriving contract. Internal visibility is not accessible from outside the contract.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract InternalVariables{
  uint age = 45;
  //age is defined as being internal to the contract. That is 
 // the default visibility.
}

Enter fullscreen mode Exit fullscreen mode


`
State variables and functions defined as internal are still available and accessible in derived contracts.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract InternalVariables{
  uint age = 45;
  //age is defined as being internal to the contract. That is 
 // the default visibility.
}

contract InheritFromInternalVariables is InternalVariables {
  uint public sum = age * 45;
   function getAge() public returns (uint ){
     return sum;
   }
}
Enter fullscreen mode Exit fullscreen mode


`

The visibility of the age variable above is internal in the contract named InternalVariables. when this contract was inherited by the InheritFromInternalVariables contract the age variable declared in the InternalVariables contract is also available.

private

A variable or function defined as private means that, that variable or function is only available for use in that contract that it is defined in.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

contract PrivateVariables{
  uint private _age = 45;
  //age is only visible on this contract. 
  //age can not be used in a contract that inherits from the 
  //PrivateVariables contract but only on the defining contract
}

Enter fullscreen mode Exit fullscreen mode


`
Declaring a variable as private does not mean it is hidden from the blockchain. anybody can still see and view your variables and function declared as private.

public

Public identifier can be applied to both internal and external contracts. The solidity compiler automatically creates a getter function for any variable declared as public. public identifier or visibility is used when you want to be able to access the variable or function from outside or inside the contract.

More on Functions

A function in solidity are methods that either change the data stored on the blockchain or they simply retrieve data. To change data on the block chain we have to send a transaction and pay gas but retrieving data is free.

Examples of function declaration
`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract FunctionsDeclaration {
  uint timeToLaunch = 567;
  function getData() public view returns  (uint){
    return timeToLaunch;
  }

} 

Enter fullscreen mode Exit fullscreen mode


`
A function defined as a view does not modify the state and so does not cost gas. The view keyword means that this function does not modify state variables.

Pure function
`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract FunctionsDeclaration {
  uint timeToLaunch = 567;
  function getData(uint x, uint y) public pure returns  (uint){
    return x * y;
  }
} 
Enter fullscreen mode Exit fullscreen mode


`
A function defined as pure means that the function promise not to manipulate or use any state variable. If we attempt to make use of the timeToLaunch state variable above the compiler will throw an error.

Multipe Return Types

A function in solidity can have more than one return value.

`

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract FunctionsDeclaration {
  uint timeToLaunch = 567;
  function getData() public view returns (uint, bool, uint){
    //this function is going to return three values to the 
   //caller
   return (455, true, 678);
  }
} 
Enter fullscreen mode Exit fullscreen mode



The return value from a function with multiple return value can be destructured like how it is done in Javascript.

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract FunctionsDeclaration {
  uint timeToLaunch = 567;
  function getData() public view returns (uint, bool, uint){
    //this function is going to return three values to the 
   //caller
   return (455, true, 678);
  }
  function applyDate() public view returns (uint){
    (uint firstValue, bool secondValue, uint thirdValue ) = getData();
    return firstValue;
  }
//firstvalue,secondValue and thirdValue represents the three //return value
} 

Enter fullscreen mode Exit fullscreen mode


`
This series we have talked about functions and variables in solidity. We examined the different visibility of variables and functions. We also talked about the return types of function and how to return multiple values from a function.

Until next time in my journey to web3 keep on coding.

Thanks for reading....

💖 💪 🙅 🚩
jamiescript
Jamiebones

Posted on December 10, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related