Solidity Basics

@Maia Maybe you are using brave browser and forgot to permission the website?

1 Like

You are absolutely right! Also, the new file has to be open under “Contracts”. It is working now :grinning: :+1: Thank you so much for your time and for saving the more advanced people of this community from a heart attack. I know you are all super busy with the Hackathon in Moralis.
Thank you Jon!
Maia

1 Like

No, I was using Chrome. Jon advised to go over Filip’s video again, and really pay attention. Finally I got it. Thank you so much for your time on answering my question, thank you!
Maia

1 Like

Shot 0002

1 Like

Hi, i’ve written down my definition of a constructor i was hoping you could confirm or modify my definition thank you -

Constructor - it runs once and is used to initialize the contract / This allows people to input data into the function– This will allow people to put in any words they want from the deploy button, instead of writing it in solidity, as code only

1 Like

Hi @RenamingMayHaveUnint & @Maia

It is still possible to use any version of Solidity in Remix. If its not allowing, or not compiling with…

pragma solidity 0.7.5;

… try manually selecting 0.7.5 from the Compiler dropdown in the Solidity Compiler panel (2nd icon down in the menu on the left of the Remix interface). Make sure you don’t have Include nightly builds checked. It’s also a good idea to check the Auto compile option under Compiler Configuration on the same panel.

The compiler should automatically switch versions according to what version you have declared after pragma solidity , but if it isn’t, try this manual selection. After that it should switch automatically for you. Having Auto compile switched on is good because it compiles as you code, allowing you to immediately see any errors and debug as you go.

1 Like

That’s a good attempt at a definition @jahh,

I’ve tried to build on what you’ve started with, and here’s my suggestion for improvement, greater accuracy, and to make it more applicable in general, rather than Remix-specific…

Constructor - it runs once when the contract is deployed. It allows the contract to be initialized according to predefined specifications that are coded within the body of the constructor e.g. setting an owner variable to whichever address deploys the contract. The constructor can also take parameters, which are input by the deployer when they deploy the contract, and which allow the deployer a degree of control and flexibility over which values certain state variables are initialized with.

I hope that crystalizes what you already know even more, and provides a useful summary :slight_smile:

Once again jon , you cleared the fog in my mind thank you

1 Like

Hi @filip, I get this error when i write this code

HelloWorld.sol:7:5: Warning: Function state mutability can be restricted to view function hello () public returns (string memory) { ^ (Relevant source part starts here and spans across multiple lines).

Code is

pragma solidity 0.7.5;

contract HelloWorld {

string message = "Hello World, It is a message";

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

}

1 Like

Hi @Gulti,

The compiler is generating this warning because your hello() function doesn’t need to modify any data in the contract state. It’s not a setter which makes a change to a state variable. Instead, it’s a getter, which only needs to read data stored in the contract state (the string stored in the state variable message ) and return it to the caller.

There are 4 function types (3 of which we define with specific keywords in the function header) as follows:

  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 be payable to send ether — only to receive it)

It is good risk management to always restrict what a function is allowed to do, to only what it needs to do. This is why the compiler is warning you that you should add view to your hello() function header, as follows:

 function hello() public view returns(string memory) { ... }

The warning will disappear if you add this.

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

Thank you @jon_m for the detailed explanation and it did work with “view”.

1 Like

Hi @jon_m, another question on get and set number

I wanted to try and use uint256 as the variable type and was expecting that negative numbers will not be accepted but when i input -10, i get -10 back.

Code

uint256 number;

function getNumber () public view returns (uint256){
    
    return number;
    
}

function setNumber (uint256 _number) public {
    
    number = _number;
    
    
}

Hi @Gulti,

Your assumption is correct. Any uint data type only allows unsigned integers i.e. >= 0 (no negative numbers). To also accept negative numbers, we need to use int

I have just compiled and deployed the exact same code you have posted in Remix, as follows:

(I add the SPDX-License-Identifier comment at the top just to get rid of the constant warning for that, which I find annoying :wink:

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.7.5;

contract IntegerTest { 

    uint256 number;

    function getNumber() public view returns (uint256) {
        return number;
    }

    function setNumber(uint256 _number) public {
        number = _number;
    }
}

And when I call setNumber from Remix with an input parameter of -10 I get the expected error message in the Remix console:  Error: value out-of-bounds.

So, there must be something that you’re not doing quite right e.g. when you’re inputting the parameter -10 and calling the function from Remix. I assume you are checking the console at the bottom of the Remix interface to see if your function call generates a transaction confirmation or an error message…?

Thank you @jon_m. I think it was an issue with Remix refresh i think. Now it works.

1 Like

Hello Filip…

Not sure if I missed it, but I’m not seeing the button with the function name “hello” on my screen (using same version of remix compiler). I have the orange/brown button called “Deploy” only, but not the “Hello” (function name) button. Still seems to work the same. Any thoughts?

Thanks!

Apologies!! Ignore… * found the reveal button.

1 Like

Hey @Acex13,

Don’t worry, these little interface things always catch us out to begin with — more than the actual Solidity code! :sweat_smile:

Is everything working smoothly now for you?

Welcome to the course! I hope you enjoy it. Just let us know if you have any questions or experience any difficulties by posting in the discussion topic for the section you’re on (just like you have done here) :slightly_smiling_face:

What is the purpose of “string memory” on line 8? Not understanding how or why this was put in there. Please assist.
image

1 Like

Why is the “view” function used on line 7, and not on line 10?
image

1 Like

Hi @Kkrypto,

string memory _message   is a parameter.
string defines the parameter’s data type. Only a string value will be accepted for the _message parameter.
memory defines the data location where the parameter’s value is stored. In the section on Data Location, you will learn about what the different data locations are, and why we only need to explicitly declare the data location for certain data types and in certain places within our contract code. Data stored in memory will only be stored temporarily. In this example, the string value input into the constructor on deployment will only be saved while the constructor is executing, and then it will be lost. However, before the constructor has finished executing, _message is assigned to the state variable message where it is saved persistently in storage. storage is another data location, but it does not need to be explicitly declared for state variables.

As data location is a fairly complex subject, I would wait until the section on Data Location for more details and clarification about this, but hopefully this general explanation is enough for you to understand why we are including memory in this particular parameter definition.

Let me know if you have any further questions :slight_smile: