Goals

The goal of this lab is to learn the following:

  • How to create actions with parameters.
  • How to create actions with return types.
  • 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 lesson, you will practice creating and calling actions using parameters and return types. Make sure you have finished Lab 4.1 first. There we have a basic introduction to actions. We will create a text game where you will create a great warrior and save vulnerable citizens! To do so, we will need input from the user. Our code will function by calling different actions. The actions will gather information and then output it in our game.

Task 1: Getting started

You can either use the IDE here, or use Quorum Studio. If using Quorum, create a new project using 'Main.'

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

    //write your program instructions here and define your variables 

    end

    //write your actions here...the program will refer to these actions when executing your code 
    //    in action Main. 

end

Task 2: Action Keywords

action GetName
    output "You're the mighty + name +"
end

Let's review the following: 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 and the end line. This is similar to the way the control structures you studied in Chapter 3 work.

As you can see we've set up an action, named GetName, and we given it a parameter... (text, name). If we left things like that our character would be called name, which would be a weird name. Instead, when we define the variable at the top let's create an input statement where we get the name from the user. See if you can remember how to do that and ask for help if you're having problems. By creating an Action with a text parameter, we are able to put in any name we want, including one from the user. This will help personalize the game and make it more fun! The more involved our user is, the more she can get out of it!

GetName(name)

In your action Main you should now have a defined text variable called name that takes an input statement. Have the input question be 'What is your warrior name!?' After that you should have written the GetName(name) action written.

Next, define a text variable called 'weapon' and have the input question be 'What type of weapon will you use?' Now, below action Main you need to create the action. Create an action called 'action GetWeapon' and give it a text parameter called weapon. Output the statement 'You will use a ' + weapon + ' to defeat your enemies!' Now to create some actions with return types. Review Lab 4.1 if needed. Let's create an action called action GetNumberOfParty. It will look like this: action GetNumberOfParty returns integer text in = input('How many will ride with you to victory!') integer amount = cast(integer, in) return amount The return type here is an integer. We're using an input statement to get the number of people in your party. The input statement, or question that will be asked of your user, only takes text, though. We'll need to cast that from text to a new integer variable, because remember our action's return type is an integer! In our action Main, we need to have the following: integer amount = GetNumberOfParty() output amount + ' brave warriors will ride with you to victory!' This defines the variable amount, an integer, and uses our GetNumberofParty action to find out how many people are in our party, converting the text input we get into an integer. Finally, following what you just did with action GetNumberOfParty, create and action called action GetCitizens. The integer variable shoul be called 'integer citint.' It should have a return type of integer. The input question should be 'How many citizens will you save?!' Your output statement in action Main should be: output 'You and your party will save ' + citint + ' citizens! Wow!' output 'You are very brave!'

 //only look at the following code if you get stuck! 

class Main
    action Main
        
        text name = input("What is your warrior name?!")        
        text weapon = input("What type of weapon will you use?")
        GetName(name)
        GetWeapon(weapon)
        integer amount = GetNumberOfParty() 
        output amount + " brave warriors will ride with you to victory!"
        integer citint = GetCitizens()
        output "You and your party will save " + citint + " citizens! Wow!"
        output "You are very brave!"
        
        
    end

    action GetName(text name)
        output "You're are the mighty " + name + "!" 
    end
    
    action GetWeapon(text weapon)
        output "You will use a " + weapon + " to defeat your enemies!"
    end

    action GetNumberOfParty returns integer
        text in = input("How many will ride with you to victory!")
        integer amount = cast(integer, in) 
        return amount  
    end 

    action GetCitizens returns integer
        text cit = input("How many citizens will you save?!")
        integer citint = cast(integer, cit)
        return citint
    end

end

Next Tutorial

In the next tutorial, we will discuss Actions with Random Noise Generator, which describes more on actions.