Solidity Basics

Good questions @M.K!

The default visibility for state variables is internal.
The default visibility for functions used to be public in older versions of Solidity. However, there is no longer any default visibility for functions, and their visibility must be stated.
For either to be private, you need to specifiy private.

Yes, if you want the state variable to be accessible for external contracts, dapp frontend interfaces AND from within the same contract… i.e. from EVERYWHERE.
If you just want it to be accessible for external contracts and dapp frontend interfaces BUT NOT from within the same contract OR from derived contracts, then you need to specifiy external visibility.

This too can take a while to “get your head round”. It is a gradual awakening … as you learn and experiment more :slight_smile:

2 Likes

** STOP PRESS **

This is out of date information. This used to be the case in older versions of Solidity. However, there is no longer any default visibility for functions: visibility must be stated.

The default visibility (where none is specified) is still internal for state variables.

Thanks for getting me thinking! :wink:

1 Like

On my study plan, the first course listed after Project 1 is React Web Development 101. Unfortunately, I got a bit tired of it and felt I just didn’t have the strength to get through the rest of it. That being said, I decided to jump ahead to Ethereum Smart Contract Programming 101. The good news is that, according to the Prerequisites video, I don’t need to have already completed the React course. The prerequisites were Blockchain & Bitcoin 101, Ethereum 101, and Javascript Programming, all of which I have completed. It has been a long time since I worked my way through those courses, but maybe the material will refresh my memory.

It just seems that a course starts out quite interesting and easy at first, but as the material builds and I’m challenged to do more hands-on activities, it just gets harder and I lose interest in it.

We’ll see how this one goes! Thanks!

1 Like

If I want to exit Remix for a certain period of time, how can I save my changes so that I can pick back up where I left off?

Thanks.

1 Like

Hi @jgentry,

These posts answer your question :slight_smile:


Conversation 1




Conversation 2



Conversation 3


1 Like

Hi @jgentry,

I hope you get on better with this course and enjoy learning Solidity :slight_smile:

That’s right, you don’t need to know React.js to do this course. However, even though there are obviously important differences, Solidity does have a lot in common with JavaScript, in terms of both syntax and concepts. Therefore, if it’s been a long time since you did the JavaScript course, I would suggest that you work through that course again, or at least some of the exercises, in order to refresh your memory, because in this Solidity course we assume that you are comfortable with what you have already covered in the JavaScript course.

However, if you would rather start something new straight away, then you could continue with this course until you feel it’s becoming too much of a struggle, and at that point return to JavaScript for some revision. However, you will only need to do that if you really hit a brick wall.

One thing that may be happening is you may be moving through courses too quickly. If you move on to the next section without having taken your time to do at least some of the following learning and study activities, then I would suggest that you need to take a slower and steadier approach…

  • Post your work-in-progress (or just part of it) here in the forum, by following the discussion topic link given to you for each section of the course. Include a specific question about a particular problem you are encountering. This can be an effective approach if you would like help before you look at the solution.
  • Have a look at other students’ posts in the relevant forum discussion topic, with their attempts, solutions, and the help, comments and feedback posted as replies. There are a lot of comments and explanations posted here. It’s well worth spending your time browsing and reading, not only to get help with the exercises, but also after finishing a section or particular assignment as a way to review what you’ve learnt, and to discover alternative coding solutions or answers.
  • When you look at the assignment solutions, if they are different to your code/answers, you should spend some time analysing and thinking about them. This is a really important stage in the learning process. You can learn a lot by working out what the solution code does and how it does it, and also how to go about “bridging the gap” from what you managed to do (or not) on your own. However, it’s important to remember that there are usually several different alternative approaches and solutions to these exercises, so if you’ve coded yours differently, but it still works, then it may well be valid. If you’re not sure, then post it here in the forum, and we’ll review it for you.
  • Another good learning technique to use (after having already done the tasks described above) is to then try the exercise again from scratch without looking back at the answer (like a memory test). You can also try to do this after having left it for a certain period of time (say, the next day, then after a few days, then a week etc. etc.) This builds up longer-term retention. This would be a good approach to take when reviewing the JavaScript course. Instead of just looking back at your solutions to the exercises, test yourself to see how many of them you can redo from scratch — only checking your previous code and notes, and rewatching the videos, if you need to remind yourself about certain things.
  • Another important point to remember is that it can take some time before you fully understand all of the code in an exercise, so another good strategy is to come back and revisit an exercise, which you didn’t fully understand, after you’ve progressed a bit more through the course. You may well find that it’s much easier then.
  • Don’t forget to do some research yourself on the Internet. Here is a link to a playlist from the YouTube channel Eat The Blocks. I’ve seen some of this guy’s videos myself and I think they are particularly helpful for learning Solidity. The videos are quite short and the explanations clearly explained and well demonstrated with clear examples. You’ll find that several of the videos in this playlist correspond to specific sections of this course, and so serve as an ideal starting point for your own further research.
    https://www.youtube.com/playlist?list=PLbbtODcOYIoE0D6fschNU4rqtGFRpk3ea
  • Finally … play around with the code, experiment, and try to come up with your own examples, no matter how basic, short or simple. Even by just making a few small changes and adaptations to the code presented in the course, any amount of personalisation that you are able to add to your code will help it to mean more to you, and will therefore help you to internalise it better.

Anyway, I hope at least some of these suggestions help make learning to programme a little less of a struggle, and more productive and enjoyable :smiley:

I entered the contract exactly as per the Contract Structure lesson (https://academy.ivanontech.com/products/ethereum-smart-contract-programming-101-2/categories/4223198/posts/14181408) but Remix still tells me “creation of Helloworld errored: can’t convert undefined to object.”
What am I doing wrong? I’ve checked everything 50x and it’s exactly as done in the video!
ffff

Hi @tenbad,

Your problem is the input parameter   function hello(string)
If you have a string parameter you also need to state memory

function hello(string memory)

We also need to give the parameter a name, otherwise we can’t reference it within the function e.g.

function hello(string memory myString)

string  is the data type
memory  is the data location (needed for strings, but not for integers, addresses or booleans)
myString  is the name of the parameter

However, your function doesn’t need an input, because you don’t need to reference a parameter in the function body. The returned output is not dependent on any inputs. It will always be the string  "Hello world".

You could, however, add a parameter, and reference it as follows…

function hello(string memory greeting) public pure returns(string memory) {
    return greeting;
}
// returned string will be whatever greeting is input when function is called. 

I’m not sure where in Remix you are seeing the error…

… but after compiling you should get a red compiler error on line 4. If you hover over that you will see the error message:
TypeError: Data location must be "memory" ... for parameter in function, but none was given.
And then it indicates that the code throwing the error is  string.

If you’re not already using it, I would switch on autocompile (tick the first box under Compiler Configuration in the Solidity Compiler). That way the compiler will highlight any errors or warnings while you code.

Let us know if anything is unclear, or if you have any more questions :slight_smile:

1 Like

Thank you so much for your help!!

1 Like

In the constructor contract i tried to put the pure back into the contract and i got an error why would it not accept it ?
pragma solidity 0.7.5;
contract first{
string message;
constructor(string memory _message){
message = _message;
}
function hello()public pure returns(string memory){
return message;

}

}

Hi i think i found the answer to my constructor question if i put view in the function header that should work correct?

1 Like

Absolutely :muscle:
Do you know why?

I thinks it’s because pure only executes once and view allows you to repeat the function

I don’t think that’s totally correct but that’s what came to mind. I know view doesn’t change the state …but i don’t know if i completely have a total mental grasp on it . I would love to hear your feed back on that
cheers

1 Like

No… that isn’t correct.

Yes, that’s correct.

The error message you would have got with pure is:

Function declared as pure, but this expression .. reads from the .. state and thus requires "view".

The error message shows that the “expression” it is referring to is  message.
In your contract  message  is a state variable which stores a string value, and so in your function it references (reads) a value stored in the contract state.

pure is the most restrictive function type. It prevents the function code from changing and reading the contract state. This is too restrictive for your function because, although it doesn’t need to change the state, it does need to read it. That’s why you needed to change pure to view

We could change your function into a pure function, as follows:

function hello(string memory message) public pure returns(string memory) {
    return message;
}

or

function hello() public pure returns(string memory) {
    return "Hello!";
}

Have a think about why…

To complete the picture for you, there are 4 function types:

  1. pure (most restrictive)
    Cannot change or read the contract state.

  2. view
    Can read the contract state, but cannot change it.

  3. undefined (no type declared in the function header)
    Can read and change the contract state (but cannot receive ether)

  4. payable (least restrictive)
    Can read and change the contract state, and can also receive ether.
    (Note: a function doesn’t need to bepayableto send ether — only to receive it)

That helps, thanks for the break down

1 Like

Hi Filip, nice to be here. Want to congratulate you on such as well-designed course. I have been here for under two months and I am an absolute beginner to programming, my aim is to become a smart contract developer and join the workforce in the immediate future. I find it very useful to contrast the differences between Solidity and JavaScript in order to consolidate what I have learned so far. That is why I am here with you at this very moment. Needless to say, I would like to get my feet wet in smart contract programming and I have a question concerning the scenario that you presented today. You used the setNumber function within the block and that means that the scope of this data is block-scope, right?
So, if the setNumber function changes the data and this data is only accessible within this scope. Why would we want to use this function?

**** Could you (could anyone) share two real-life examples associated with a real task that programming a smart contract might entail and that we might need to confront where we would want to use a setNumber function, please? *** What I am trying to connect is how this specific output could interact with the rest of the contract. Specifically, how it might be used by a specific function in a different part of the contract. ******

Thanks in advance for shedding light on this topic.

1 Like

Hi guys,

Does anyone know how to interact with instance just like Filip does with remix on VS
Code?Capture

Thanks,

Leo

Hi everyone, excited to be learning Solidity!

I have a newbie question on Remix itself - when I want to type in between code I’ve already written, it overwrites the code that comes after rather than inserting it. It’s very annoying, does anyone know how to prevent this from happening?

Thanks!
Jazmin

1 Like

Hi Jaz, press hold the insert key it should resolve the issue

2 Likes