Tutorial: Actions

How to tell a program to take a behavior.

Actions in Quorum

In the Quorum programming language, actions are behaviors that a program can take. They are useful for many reasons, including at least that they 1) allow us to reuse code, 2) can sometimes make it easier to separate out functionality in complex software, and they 3) allow us to break up a program's functionaity between different people on a team.For example, suppose we wrote an action to add numbers together. We might start with a definition:

action Main
end

action Add
end

Notice in this case that there are two copies of the word action, each with a name (e.g., Main and Add) after the word. Then, in each case, the word end comes after our name. By default, Quorum requires that all programs begin at the action Main. Until now, we have not been using actions and, as such, did not have to write this. Now, in order for our program to execute, we must define both. In terms of what this program does, while it is valid, note that it has no output.

Notice a few conventions in Quorum. First, action names start with a capital letter. Second, we often match the word end at the same indentation level as the word action. Quorum programs will still work if these conventions are not followed, but the standard library uses them and most programmers using Quorum, to our knowledge, follow them. Next, let us add an output statement to the Add action and note what happens

action Main
end

action Add
     output "Hello, Actions!"
end

Now, we might assume that this code will execute and output our little phrase, but it does not. The reason is because all programs start at Main, but our Main action starts and immediately exits without calling Add. Let's fix that problem:

action Main
     Add()
end

action Add
     output "Hello, Actions!"
end

In this case, Quorum starts our program by calling the action Main, then immediately notices that we are asking it to call the action Add. Once the program jumps to this position, it executes the output statement, finishes the Add action, jumps back to Main and terminates the program. Now our Add action does not actually add anything, but we will fix that next.

Action Parameters

We often need to give information to our actions in order for them to compute information for us. A simple example might be adding two integers. In this case, we would want to pass information, each integer, and then get information back on the result (called returning). In this section, we cover passing the information and in the next we cover returning it. First, let's modify our Add action to take two parameters:

action Main
     Add() //this is an error
end

action Add(integer a, integer b)
     output "Hello, Actions!"
     output a + b
end

In this case, the above code gives us an error. The reason is that, while we have modified the Add action with parameters, we have not changed how it is called. It is sometimes good practice to run code with an error in it, so that we can learn to recognize the mistake. We fix the error next.

action Main
     Add(1, 2)
end

action Add(integer a, integer b)
     output "Hello, Actions!"
     output a + b
end

We now have an action that can compute an addition, but this action is not very general. It outputs the value to the screen, but does not provide the information back to the part of the program calling it in any useful way. In order to get information back, we need to return the value.

Returning a value from an action

Actions can take many forms in computer science, from addition, like what we have been writing to very complex ones, like certain mathematical operations. It is common in programming languages for many of these actions to be written in to a standard library. For us to use them, however, we might need these actions to give us back information that we can use.

action Main
     integer result = Add(1, 2)
     output result

     result = Add(3, 4) //notice we can reuse the code
     output result
end

action Add(integer a, integer b) returns integer
     return a + b 
end

The action Add now has two words at the end of the right paren, labeled returns integer. This code means that the action must send back an integer value to the action that asked us to do the addition. Next, in order to send back the value we use the word return and then whatever computation we want. In this case, that is the addition. We can then use these results to print out two values, 3 and 7.

As one final example, it is also possible to exit an action, even if we do not return a value. This process can only be used if the action returns nothing. Here is an example with this statement, return now, inside of a loop.

action Main
     RunMe()
     output "The End."
end

action RunMe
     repeat while true 
          //this statement stops the loop 
          return now
     end
end

Next Tutorial

In the next tutorial, we will discuss classes, which describes how to organize our code using abstraction.