[SOLVED] Why is a retrieve function introduced here when the contract already provides this?

Issue

This Content is from Stack Overflow. Question asked by ariovistx

this is my first post and I am pretty new to programming and working my way through learning solidity with the help of freeCodeCamp on Youtube right now.
At around 1:48:00 in this video (https://www.youtube.com/watch?v=M576WGiDBdQ) the author introduces the retrieve function which basically has the same functionality of the statement in contract, right? When both of them lead to the same result, why would using the retrieve function be necessary in this case? Isn’t this function just going to waste space? Or which advantages does it provide? Unfortunately he doesn’t explain it and it’s confusing me like hell.
Kind regards

contract SimpleStorage {

uint256 favoriteNumber;

function retrieve() public view returns (uint256) {

return favoriteNumber;

  } 

}



Solution

Storage variables default to internal visibility if you don’t specify the visibility. So without that function you don’t have a public getter for the variable.

You could remove the need for the view function by simply declaring the variable with public visibility:

uint256 public favoriteNumber;


This Question was asked in StackOverflow by ariovistx and Answered by C S It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?