Chapter 1: Elementary

Lesson 5: Variable declaration in a transition

We can also declare variables within a transition. Please note that these variables are not immutable variables nor are they mutable variables like the ones we learned about earlier even though the values of these variables that exist within a transition can also be changed. This is because their value is never stored on the blockchain. Rather they are temporary variables whose scope is limited within the transition for the duration of that single call.

The temporary variable declaration format is different from the format in which the mutable variables are declared.

For instance, a String variable is declared in the given format.

Note: In a transition, if there are multiple lines, then they are separated by semicolons. Let’s take a look at an example of a transition with two variables declared in it:

In this example, we declare two variables in a transition named foo. The first variable is a string type variable that stores the value hello. The second variable is an integer type variable that stores the value 5.

Notice two points in this example:


  • The two lines within the transition are separated by a single semicolon. We don’t use a semicolon after the declaration of the variable b because there are no further lines in the transition seen in the example. Those who are familiar with other computer languages might be used to using a semicolon to end a line of code. However, here the semicolon can be thought of as being used in the capacity of a conjunction rather than a period, so just like we won’t normally use the conjunction ‘and’ at the end of a sentence, similarly we don’t use the semicolon here at the end but instead to join the various lines.
  • The format of the declaration of a string variable is different from the format of declaration of an integer variable. You can find the full list of the declaration formats of all the major variable types (in the main body of the contract, and in the transition) in the Cheat Sheet in the Task section. However, for the sake of the exercise at the hand, you can just refer to the content provided in this lesson.
  • We have a task for you!

    We want to replace the original user name declared in mutable variable username in this transition. To do that, we will need a new name.

    Declare a new variable with the name newname and type String and give it a value Bob.

    Your Workspace

    Show Solution

    Solution

    1

    2

    3

    4

    5

    6

    7

    8

    9

    scilla_version 0
    contract SocialMediaPayment (owner: ByStr20)
    field username: String = "Alice"
    transition changeName()
    (* Start typing from the line below *)
    newname = "Bob"
    end
    scilla_version 0
    
    contract SocialMediaPayment (owner: ByStr20)
    field username: String = "Alice"
    
    transition changeName()
      (* Start typing from the line below *)
    end
    1
    2
    3
    4
    5
    6
    7
    8