Goals

The goal of this lab is to learn the following:

  • How to create actions with no parameters or return types
  • How to create actions with parameters.
  • How to create actions with return types.
  • How to call actions without parameters
  • How to call actions with parameters
  • How to utilize returned values from actions

Computer Science Principles Curriculum

  • Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1

Common Core Standards

  • English Language Arts Standards ยป Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8

Overview

In this lab, you will focus solely on how to create and call actions. You will start out by creating the simplest possible action, one with no parameters or return values. Then you will work your way up to the creation of actions that have both parameters and return values. The goal of this tutorial is to master the creation and usage of actions, and to learn good programming practices for actions.

Task 1: Getting started

First, start Quorum Studio. You will create a new project; however, you will not be creating a blank application this time. Instead, in the New Project dialog, select "Main" and name the project FunWithActions. You can also write your code in the built in IDE below.

When your project opens in Quroum Studio, Main.quorum will contain the code listed below. You will be typing your code in two different locations--between action Main and the first end, and between class Main and the second end.

class Main
    action Main
    end
end

Task 2: Creating Basic Actions

First, you will focus on the creation of actions that have no parameters or return value. These type of actions can help break up a program into logical sections. For example, let's say you want to greet the user, and then ask them for the balance in their bank account, and then exit. In theory, these operations don't require a lot of code, but over time, as you want to add more to these operations, it may become confusing to read the code. Let's demonstrate the use of actions for these two operations. First, you will create a method called GreetUser like so:

action GreetUser
    output "Hello there!"
end

When you want to create an action, you must first use the action keyword. This keyword lets Quorum know that you preparing to declare a new action. Following the action keyword is the name of your action. Actions obey the same naming rules as variables--any valid variable name is a valid action name. Action names are usually capitalized. Also, don't forget to use camelcase if your name is more than one word! After you state the name of the action, you place the code you want inside the action between the action GreetUser line and the end line. This is similar to the way the control structures you studied in Chapter 3 work.

Inside the Main action, let's add code to call this action. When you "call" an action, Quorum executes all of the code in that action (the code between the action GreetUser and end lines, in this case), and then comes back to the place in the code where you called from. To demonstrate how this works, enter the code below into the Main action.

GreetUser()
output "Back from GreetUser()."

Your code in the editor should now look like the following:

class Main
    action Main
        GreetUser()
        output "Back from GreetUser()."
    end
    action GreetUser
        output "Hello there!"
    end
end

Run the program. In the Output Window, you will now see the following:

Hello there!
Back from GreetUser().

What happened here is that Quorum recognized you were calling an action by the syntax of the GreetUser() line. When you specify a name followed by an opened and closed parentheses, Quorum assumes you wish to execute the code in that action. In this example, Quorum executed the code inside the GreetUser action, and then came back to execute the next line, output "Back from GreetUser()." Make sure you understand the flow of the program and how it's jumping from line to line. To go more in depth, run the debugger now and step through the code to get an idea of how this program executes. Try stepping into the GreetUser() line of code.

Let's create one more action, called GetUserBalance, that asks the user to enter a dollar amount for a bank account. This action will then output the value the user entered to the screen. The code for this action is shown below.

action GetUserBalance
    text in = input("How much money is in your savings account?")
    output "You have $" + in + " in your savings account."
end

You will also need to add code to your Main method to call this action. Your main.quorum file now looks like so:

class Main
    action Main
        GreetUser()
        output "Back from GreetUser()."
        GetUserBalance()
        output "Thank you!"
    end
    action GreetUser
        output "Hello there."
    end
    action GetUserBalance
        text in = input("How much money is in your savings account?")
        output "You have $" + in + " in your savings account."
    end
end

Notice that you called GetUserBalance in the exact same way you called GreetUser previously.

Run the program. Your program will prompt You to enter a dollar amount. After you do, you will see something similar to the output below in the Output Window.

Hello there.
Back from GreetUser().
You have $1.00 in your bank account.
Thank you!

You will now tackle an extremely powerful feature of actions, called parameters.

Task 3: Using Parameters

An extremely useful feature of actions is that they allow you to pass in what are called parameters. In other words, you can make the program behave certain ways when certain information is passed into those variables. Parameters are variables, much like the variables you discussed in chapter 2, that are available inside the scope of the function, but who's values are not explicitly stated in the action. So far, all of your actions have simply executed the code inside of them and returned. Parameters allow you to make this behavior much more interesting by making the behavior change based on the input to the action.

That sounds a bit complicated, so let's create an example to demonstrate. Earlier, you created a GreetUser action, that printed the message "Hello there!" to the screen. You could make this action slightly more friendly by passing it a parameter telling you the name of the user. You define such an action below.

action GreetUser(text name)
    output "Hello there, " + name + "!"
end

Notice that the first line of this action is different from the previous GreetUser action. Instead of simply ending the line after the GreetUser name, you use an open parenthesis to tell Quorum you are going to specify parameters. In Quorum, parameters are always specified between a set of parenthesis. Once you have specified your parameter, you close the opened parenthesis. You have added one parameter to this action, called name, of type text. By doing so, you make the variable name available for use in your action. Multiple parameters can also be specified, separated by a comma (covered in section 5). You use it in the output statement on the next line. Notice that you did not initialize the variable name before using it in output. Rather, Quorum took care of this for you , and you can assume it already has some value.

Place this code into the editor and remove the old GreetUser action. Run the program. Notice that you receive the following compiler error:

I could not find an action named 'GreetUser' in the class 'Main'

You receive this error because your previous line for calling GreetUser does not specify a value for the name parameter. To correct this, you can change the line to look as follows:

GreetUser("Jeff")

Like before, you are still using an open parenthesis after the action name to indicate that the action will be called. However, you are no longer leaving it empty--instead, you are specifying a text value. Feel free to replace "Jeff" with your own name. Your full program will now look like this:

class Main
    action Main
        GreetUser("Jeff")
        output "Back from GreetUser()."
        GetUserBalance()
        output "Thank you!"
    end
    action GreetUser(text name)
        output "Hello there, " + name + "!"
    end
    action GetUserBalance
        text in = input("How much money is in your savings account?")
        output "You have $" + in + " in your savings account."
    end
end

When you run this program, the first line of output now looks like this:

Hello there, Jeff!

When Quorum ran your GreetUser action, it printed the value you put into quotations above. Experiment by changing the line GreetUser("Jeff") to have other names in quotations. You are not limited to constant values, either. You could also pass in a variable, like in the code below. The only restriction is that the value you pass in must be of type text, as that is the type you specified when you created the action above.

text myName = "Jeff"
GreetUser(myName)

Parameters are an extremely powerful feature of actions that have quite a bit of application in computer science. In the next section, you will cover the final important property of actions: return values.

Task 4: Return Values

So far, you have covered how to create actions with and without parameters, and how to call these actions. An important feature of actions that you have so far not discussed is the concept of return values. Return values can be thought of as the "result" of an action. For example, if you created an action that added two numbers, say 3 and 4, the result of this operation is 7. Like parameters, return values are best demonstrated by example.

One way you might utilize return values in your existing program is to change the GetUserBalance action. Currently, this action asks the user for their balance, and then prints it to the screen. If you wanted to use this value later in the program, say, outside of the GetUserBalance action, you couldn't; it is simply lost when the GetUserBalance action ends. Let's change this action to return an integer containing the dollar amount the user enters. To do this, first remove the old GetUserBalance action, and then add the following code:

action GetUserBalance returns integer
    text in = input("How much money is in your savings account?")
    integer amount = cast(integer, in)
    return amount
end

This code introduces you to two new keywords. On the first line of this code, you see the keyword returns. This keyword tells Quorum what type your action will return. The second keyword, return (on the third line), actually specifies what you are returning. In this case, your action returns a type of integer, and returns the variable amount, which contains the dollar amount the user entered. Also, notice that you entered text, and we later casted that text to an integer.

Before you continue, run the program. Your program no longer prints the value that the user entered. However, unlike before, you can now retrieve the value that the user entered and use it outside of the GetUserBalance action. You can utilize its return value by calling the action in a slightly different way. When an action returns a value, the action can be used in any expression that requires that type. Let's define an integer variable amount, and assign it the return value of GetUserBalance:

integer amount = GetUserBalance()
output "You entered $" + amount

Notice that, like before, you are calling GetUserBalance using the opened/closed parentheses syntax. However, to the left of this code, you are performing an assignment to the variable amount.

Your full program now looks like this:

class Main
    action Main
        GreetUser("Jeff")
        output "Back from GreetUser()."
        integer amount = GetUserBalance()
        output "You entered $" + amount
        output "Thank you!"
    end
    action GreetUser(text name)
        output "Hello there, " + name + "!"
    end
    action GetUserBalance returns integer
        text in = input("How much money is in your savings account?")
        integer amount = cast(integer, in)
        return amount
    end
end

When you run this program, your output now looks like so:

Hello there, Jeff!
Back from GreetUser().
You entered $33
Thank you!

Task 5: Putting It All Together

Now that you have covered the basics of actions, parameters and return values, let's try putting all of this together and create an action with parameters and a return value. Let's create a couple of actions, multiply and divide. You will walk through the creation of the multiply action step-by-step, and leave the divide action up to you , with a word of caution.

To start out, let's erase all of the code in the editor so that it again looks like it did when you started:

class Main
    action Main
    end
end

When designing actions, it is important to consider the input of the action and any potential results. In the case of the multiply action, you need to pass it two numbers, a and b, and your result also needs to be a number. Given this, you wind up with the action below:

action Multiply(number a,  number b) returns number
    number result = a * b
    return result
end

Notice that this action is different than others you have studied so far, as it accepts multiple parameters. When specifying multiple parameters, you separate each parameter with a comma. Let's also add code to call this action from Main, as in below:

number k = Multiply(2, 4)
        output k

Now, your code in the editor looks like the following:

class Main
    action Main
        number k = Multiply(2, 4)
        output k
    end
    action Multiply(number a, number b) returns number
        number result = a * b
        return result
    end
end

When you run this code, the value in the variable k will be 8.

For the final portion of this lab, write a method, called Divide, that takes two parameters of type number, a and b, and returns the type number. Note that division by zero is not allowed. Write a method that performs division, but returns the number 0.0 when division by zero will occur. Use your knowledge from Chapters 2 and 3. Hint: Make use of the if construct.

To check your work, here are a few examples of input and the values that will be returned.

number k = Divide(2, 4)

The value of k should be 0.5.

number k = Divide(9, 3)

The value of k should be 3.0.

number k = Divide(6, 0)

The value of k should be 0.0. (division by zero has occurred).

Next Tutorial

In the next tutorial, we will discuss Actions Lesson, which describes more on actions..