SimpleStorage: difference between name and _name #1078
-
|
Hello! I'm just wondering why was the variable for name changed... under the struct it was "string name" but when creating the feature to add into the array people it became "string _name" What does the underscore indicate? Is this a completely different variable? I hope this question makes sense. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
|
You didn't provide the repository you are referring to. Which lesson are you in right now? Many use a variable with a prefixed underscore for function parameters. Here is an example: Here you can see to types of variables:
Does that answer your question? |
Beta Was this translation helpful? Give feedback.
-
|
Hello @pharmacyhacks struct People {
uint256 favoriteNumber;
string name;
}
function addPerson(string memory _name, uint256 _favoriteNumber) public {
people.push(People(_favoriteNumber, _name));
nameToFavoriteNumber[_name] = _favoriteNumber;
}I'm pretty much sure you are referring to this code, and the answer is the following. Using under score on functions or variables means nothing to he compiler itself but is a convention for good practices at coding, the underscore means that function/variable can only be called from itself, in other words I does no mean that Also if you use some linting tool like I hope this info might be helpful. Cheers. |
Beta Was this translation helpful? Give feedback.
-
|
Ah sorry about that, I'm in Lesson 1 - Adding an Array @n4n0b1t3 @cromewar thank you for the responses! Yes I was referring to this code: struct People { function addPerson(string memory _name, uint256 _favoriteNumber) public { OK underscore means nothing to the compiler and is meant to help understand the code better. So the compiler knows that the variables in the struct (favoriteNumber and name) are inputted from the variables from people.push (_favoriteNumber, _name)? lol maybe I'm overthinking this too much |
Beta Was this translation helpful? Give feedback.
Hello @pharmacyhacks
I'm pretty much sure you are referring to this code, and the answer is the following.
Using under score on functions or variables means nothing to he compiler itself but is a convention for good practices at coding, the underscore means that function/variable can only be called from itself, in other words
_nameexists just on the context of the function and can be called just on that specific function.I …