Function and Variable Ordering

Each individual Vyper smart contract consists of a single Vyper file only. In other words, all of a given Vyper smart contract’s code, including all functions, variables, and so forth, exists in one place. Vyper requires that each smart contract’s function and variable declarations are physically written in a particular order. Solidity does not have this requirement at all. Let’s take a quick look at a Solidity example:

  1. pragma solidity ^0.4.0;
  2. contract ordering {
  3. function topFunction()
  4. external
  5. returns (bool) {
  6. initiatizedBelowTopFunction = this.lowerFunction();
  7. return initiatizedBelowTopFunction;
  8. }
  9. bool initiatizedBelowTopFunction;
  10. bool lowerFunctionVar;
  11. function lowerFunction()
  12. external
  13. returns (bool) {
  14. lowerFunctionVar = true;
  15. return lowerFunctionVar;
  16. }
  17. }

In this example, the function called topFunction is calling another function, lowerFunction. topFunction is also assigning a value to a variable called initiatizedBelowTopFunction. As you can see, Solidity does not require these functions and variables to be physically declared before being called upon by the excecuting code. This is valid Solidity code that will compile successfully.

Vyper’s ordering requirements are not a new thing; in fact, these ordering requirements have always been present in Python programming. The ordering required by Vyper is straightforward and logical, as illustrated in this next example:

  1. # Declare a variable called theBool
  2. theBool: public(bool)
  3. # Declare a function called topFunction
  4. @public
  5. def topFunction() -> bool:
  6. # Assign a value to the already declared function called theBool
  7. self.theBool = True
  8. return self.theBool
  9. # Declare a function called lowerFunction
  10. @public
  11. def lowerFunction():
  12. # Call the already declared function called topFunction
  13. assert self.topFunction()

This shows the correct ordering of functions and variables in a Vyper smart contract. Note how the variable theBool and the function topFunction are declared before they are assigned a value and called, respectively. If theBool was declared below topFunction or if topFunction was declared below lowerFunction this contract would not compile.