# 🦄 First function for every developers: "Hello World" with Solidity

I've been having an interest in Solidity for a while and there is no better time to start than now! So enough talking.



## ✨ Solidity


> 
Solidity is a statically-typed curly-braces programming language designed for developing smart contracts that run on Ethereum. 

from  [Solidity document]([https://soliditylang.org/) 



## 🚀 Getting Started


Now we are going to start diving in to coding. 

Firstly, go to  [Remix IDE](https://remix.ethereum.org/). This is a code editor for coding, deploying and debugging smart contracts without having to install anything on our computers. Think of it like a playground for smart contracts.

![remix.ethereum.org_.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1637600821566/_uqP6sbxU.png)

Then click on the little file icon under the default_workspace to create a new file. We are going to name it *Hello.sol*, as you can guess .sol is for Solidity file. And put in these code.


```
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.7.0 <0.9.0;

contract Hello {
    
    function greeting () public pure returns (string memory) {
        return "Hello there";
    }
    
}
``` 

We'll go through every lines step by step.

***// SPDX-License-Identifier: UNLICENSED***

This is a machine-readable  [SPDX license identifiers](https://spdx.dev/). It will be the first line of every smart contracts. The compiler will give us a warning if it's not there. If we don't want to specify a license or if the source code is not open-source, just use the special value *UNLICENSED.*

***pragma solidity >=0.7.0 <0.9.0;***

Source files should be annotated with a version pragma so the files won't be compiled by a compiler that does not meet the version requirement. In this one it works on a compiler starting with version 0.7.0 and less than 0.9.0.

***contract***

A contract is a collection of code (functions) and data (variables) that resides at a specific address on the Ethereum blockchain. It works like a class in other programming languages.


***function greeting () public pure returns (string memory)***

Here comes our first function! There are several parts and might look confusing at first, I went WTF the first time I saw this. But after breaking it down, it is easier to split into 5 parts.

1.  *function*  : This is a keyword for defining a function.

2.  *greeting ()* : A function name. Inside the parantheses is its parameters which we don't have for now.

3.  *public* : In Solidity, functions are public by default. This means anyone can call our contract's function and execute its code. Another keyword is *private*, using private and other contracts cannot call this function.

4.  *pure* : Function modifiers. We use *pure* in this one because this function doesn't even read from the state of the app — its return value depends only on its parameters. The example values are:

       **pure** : Pure functions, which means it's not even accessing any data in the app.
       **view** : View function, meaning it's only viewing the data but not modifying it.


5.  *returns ()* : What the function returns. Noticing the *memory* keyword? This is for reference type parameter in Solidity.



What is a reference type? There are 2 ways we can pass an argument to a Solidity function:


> 
By value, which means that the Solidity compiler creates a new copy of the parameter's value and passes it to the function. This allows the function to modify the value without worrying that the value of the initial parameter gets changed.


> 
By reference, which means that the function is called with a reference to the original variable. So, if it changes the value of the variable it receives, the value of the original variable gets changed too.


## ✔️ How can we test?


Ctrl + S to save and go to deploy menu in the left side bar. It will show some information about our contract. We don't need to set anything for now, so just click the deploy button. After it's done we can see that our Hello contract appears in the *deploy contracts* section. Click on the arrow on the left side of the contract's name and it will reveal the *greeting* button. Remix even creates this for us for easily testing!

![Pasted_Image_22_11_2564_BE_23_41.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1637600837778/Txvy1jA4F.jpeg)


Click on the button and... our work is done!

![Pasted_Image_25_11_2564_BE_00_20.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1637774531841/3tqxo3p_8.jpeg)
![Hello there](https://media.giphy.com/media/Nx0rz3jtxtEre/giphy.gif)


But not so soon.



Function that return static responses wouldn't do. Let's mofidy the greeting function a bit...


```
 function greetingWithName (string memory _name) public pure returns (string memory) {
        return string(abi.encodePacked("Hello there", " ", _name));
  }
``` 

Now we get the function that can accept a string as a parameter and returns it with the previous sentence. Save and deploy again and now we can 'hello there' to Obi-Wan as many times as we'd like.


![Pasted_Image_25_11_2564_BE_00_25.jpg](https://cdn.hashnode.com/res/hashnode/image/upload/v1637774784059/IBSnoYO35.jpeg)


**Note**

- It is best practices that the parameter name is started with an underscore like _variableName, so it can easily distinguish the scope of the variables.

- *abi.encodePacked* is a function to append a string to another.


<hr />

And that's it for now. Thanks for dropping by. Hope this help introduce some basic Solidity. The next article will be more in dept about states. Stay tuned!




