Vyper Beginner's tutorial: Syntax and Structure.
Tags: webdev, web3, vyper, smartcontract
Published: 2024-05-20 13:48:29

## What is Vyper?

[Vyper(vyperlang)](https://docs.vyperlang.org/en/stable/index.html) is a python-like programming language for writing smart contracts on the Ethereum blockchain just like Solidity.
It is a statically-typed language, which means that the type of each variable must be explicitly declared. This helps to prevent type-related errors and makes the code **easier to read and understand.**

[Vyper](https://docs.vyperlang.org/en/stable/index.html) also has a number of features that are designed to improve the security of smart contracts. For example, Vyper does not allow for low-level operations, such as direct memory access, which can be a source of vulnerabilities in smart contracts. Vyper also has **built-in support for security features**, such as reentrancy guards and overflow protection.

This makes Vyper a popular choice among developers whose major emphasis is on security, readability and simplicity.

During the course of this tutorial, our major emphasis is going to be on the basic syntax and structure of a vyper contract.

## Prerequisites:
1. Basic Knowledge in Python or any other programming language.

2. A well set up development environment([i recommend one of my previous articles for this purpose](https://dev.to/mosesmuwawu/writing-smart-contracts-using-vyper-remix-ide-and-deploying-them-on-bnb-smart-chain-8o8))

3. And lastly, we shall need to interact with our smart contracts using web3.py( [Again,  i recommend my previous article for this purpose](https://dev.to/mosesmuwawu/how-to-interact-with-smart-contracts-locally-using-web3py-2896))

Well! With all the above in place, we are now good to go.

## Syntax and Structure:

Just like any other programming language, vyper also has several specific syntax rules that you should be aware of. These rules help ensure code readability, security and simplicity I will advise you to visit the [vyperlang](https://docs.vyperlang.org/en/stable/index.html) official documentation for more information about the vyperlang syntax.

As you might have noticed, learning the syntax is one thing but also applying the knowledge is another. Therefore, let's use a simple smart contract example;

```python
# A simple Vyper contract
@external
def sayHello() -> String[13]:
    return "Hello, World!"

```

2.  I will start with the first line of code:

```python
# A simple Vyper contract
```
This is simply a line of  comment in our vyper code. Such code is ignored by the compiler and therefore, it's always meant to provide explanation to human understanding of the code.

3. ```python
   @external
   ``` 
This is a decorator that specifies that the following function is part of the public interface of the smart contract. It means that this function can be called from outside the contract, such as by other contracts or users interacting with it.

Please note that all functions must include one [visibility decorator](https://docs.vyperlang.org/en/stable/control-structures.html#decorators-reference) `(@external or @internal)`. The remaining decorators are optional.

4. ```python
   def sayHello() -> String[13]:
   ```
- `def` is a keyword used to define a function.
-  `sayHello` is the name of the function. You can call this function sayHello to execute the code within it.
- `():` The parentheses are used to list parameters if the function takes any. In this case, `sayHello` does not take any parameters.
- `-> String[13]:` This part specifies the return [type](https://docs.vyperlang.org/en/stable/types.html#types) of the function. It indicates that the function will return a string of a maximum length of 13 characters.

5. ```python
   return "Hello, World!"
   ```
The function body contains the code that is executed when the  function is called.
- `return` is a keyword that specifies the value to be returned by the function.
- `"Hello, World!"` is a string literal, and in this case, it is the value that the function returns. The length of this string is 13 characters, matching the specified return type `String[13]`. 

## Conclusion
Vyper is a very secure and simple programming language meant for writing smart contracts targeting the Ethereum Virtual Machine(EVM). We meet in the next tutorial about [variables](https://dev.to/mosesmuwawu/vyper-beginners-tutorial-variables-1eo2) If you found this post helpful, please like and share it to others. Any kind of interaction in the comment section will do me good and i am open to any kind of opinion. Thank you.