Alice+Tut5++Methods+Vs.+Functions


 * ~ home ..... Getting Started ..... ||~ Tutorials and Commands ..... ||~ resources ||

Methods vs. Functions

Alice and other implementation languages support both existing and user-defined functions. Existing functions are already available for you to use. For example, as shown below, we already used several existing functions in Alice including “ask user for a number”, “minimum of a and b”, and “square root of a”. These functions are available in the World details. Other functions are readily available for each object that you bring into the World. A programmer should always try to use an function that already exists, if it meets their requirements, prior to creating their own function.

User-defined functions are functions that a user creates to fill special needs or functionality that does not already exist. We create functions in Alice by selecting the “create new function” button under the functions tab in the objects details. We will concentrate user-defined functions in this document. **Functions versus Methods in Alice:** As you probably noticed, Alice has a methods tab in the object details area. Alice supports both functions and methods. You will quickly see they are similar. The main difference is functions in Alice always return a variable where as methods do not. As you experiment with other implementation languages such as Java and C++ you will see the term methods is used more frequently and these methods can also return variables and objects. So for now, realize Alice differentiates functions and methods based on what is returned to the caller but other languages may not. You will also see the term methods and functions often interchanged in the literature. In most languages functions have the general form of: variabletype functionname (parametertype, parametername,…) Where variabletype is the data type (e.g., Number, String, Boolean) that is returned from the function, parametertype is the data type sent into the function and parametername is the name of the parameter sent into the function. Each function may have multiple parameters sent into the function. For example the following function takes two numbers, calculates their average and returns a number. number  calculateAverage( number  number1, number  number2 ) Keep in mind, other implementation languages may use other variable types such Integer, Float, Double, Long and others. In Alice, methods are similar in that you can create them and send in parameters but no values are returned.

An overview of the actual function and how we would call it is as follows: number  calculateAverage( number  number1, number  number2 )
 * 1) **Let’s get started with creating a simple function that calculates the average of two numbers.**

{

 number  average;

<span style="color: #000000; font-family: 'Courier New',serif; font-size: medium;">average = (number1 + number2) / 2.0;

<span style="color: #0000ff; font-family: 'Courier New',serif;">return <span style="font-family: Courier New,serif;"> average;

<span style="color: #000000; font-family: 'Courier New',serif; font-size: medium;">} <span style="color: #008000; font-family: 'Courier New',serif; font-size: medium;">//To call the function: <span style="color: #0000ff; font-family: 'Courier New',serif;">int <span style="font-family: Courier New,serif;"> avg = calculateAverage(78,63);

1. As before, we need to start Alice and load an object to visualize. Using your file manager system (explore in Mac) open the folder where you installed Alice. Open up Alice and start with a blank (default) world.

2. Use the green **ADD OBJECTS** button to bring in an object of your choice for visual display purposes, i chose the gorillarobot.



3.After you have closed the gallery by clicking the **DONE** button, you can display the functions of your object by clicking the object you just added in the world frame found on the upper left portion of the screen and selecting the functions tab.



4. To create a new function click on the **create new function** button. When the window pops-up enter the function name of **calculateAverage** and make sure the return type is **Number**.



5. Click **OK** and a template for the function will appear in Alice. Notice the function is listed in Alice as Objectname.calculateAverage. Since my example uses the gorillarobot object my function is called gorilla robot.calculateAverage.



6. Using the penguin.calculateAverage tab, let us first create two new parameters called number1 and number2. These will serve as the two numbers that are sent into the function. To do this, we just select the **create new parameter** button and enter the appropriate names. We do this twice, once for each parameter.

7. After we push OK for each number parameter, our function now lists two parameters appropriate labeled number1 and number2. a **parameter** is a special kind of [|variable], used in a [|subroutine] to refer to one of the pieces of data provided as input to the subroutine. [|[1]] These pieces of data are called **arguments**. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call can be assigned to the corresponding parameters.

8. Next, we will create a number variable to assign the average calculation. Eventually this is the variable that we will return. To do this we just select the **create new variable** button, select the data type as number and name it average.

If you are following the steps along and comparing them to our function design, you should see we are almost done. All we need to do is provide the calculation of (number1 + number2)/2.

10. To do this drag and drop the average variable to the first line of the function and then set its value. Initially, we set the value to the number1 parameter using the expressions option. We will add to this value to make the calculation correct in the next couple of steps.

11. Our calculateAverage method now looks like this:

12. We use Alice’s drop down menus to add the remaining of the equation. We first select the drop down arrow next to the number1 and select math to enter the + operator and finally we add the number2 parameter.



13. The results of this action will look similar to this:

14. As you can see we still aren’t quite there yet. We need to divide the sum of number1 and number2 by 2 to correctly calculate the average. To do this, click on the drop down arrow after the summation parenthesis and select math, (number1 + number2)/ and then 2. The Alice interface and how you use drop down selections should be becoming quite comfortable by now.



Our function now looks like this:

15. The last step we need to perform before we start calling and testing our function is to make sure we are indeed returning the average. Right now, we are just returning the default value of 1. To return the average we calculate, just drag and drop the average parameter found at the top of the function onto the return value. Our new function now looks like this:

16. Our function looks good but we have to call it to and test it to make sure it is indeed calculating the average correctly. To do this, let’s select the world.my first method tag and begin to construct a couple test calls to our function.



17. By default there is no code in the first method so let us add some to test our new function. We need to create a few variables to do the testing. At a minimum, we need two numbers to send into the function and one number to assign the returned variable. So let us create two numbers and call them testnumber1 and testnumber2.



18. This results in our first method looking like this:



19. Next, we create the testaverage variable that will actually be holding our average returned from calling the calculateAverage function.

20. Now we should see all of our variables for our first test for our function.

21. Next, we need to call our new function in our first line of code. To do this we start by dragging and dropping our testaverage variable and then setting its value to the gorillarobot.calculateAverage function. We use the drop down arrows to do this by selection expressions and the actual function we want to use.

22. This results in the following line of code in Alice.

23. Notice Alice assigned default values of 1 for our input parameters of number1 and number2. For our first test this might be fine as if it should display the average of 1 and 1 which should be 1. To test this, let us add a print testaverage line by dragging and dropping the print function and assigning the testaverage variable. You use the drop down arrows to select **my first method** objects.

and it should look like this:

24. We can go ahead and test this first set of data by clicking the **Play** button. The first test seems to yield the correct results.

25. Let’s test a few more combinations. Let’s first modify the function call and actually use our testnumber1 and testnumber2 variables we created. To do this just drag and drop the variables to the function call. Recall the box turns green when you are properly aligned with an object that can accept your substitution value. After successfully, dropping these variables into the function call, our method in Alice looks like this. Notice testnumber1 and testnumber2 are now being used as the parameters sent to the calculateAverage function.

26. Now, we can use the drop down arrows for testnumber1 and testnumber2 to change to any numbers we want to test. Let’s test with testnumber1 = 4 and testnumber2 = 8. If we run this again by selecting the **Play** button our functions does indeed return out expected results of 6.

27. You could also modify the World’s first method to ask the user for two numbers. This way you could test many different combinations without having to change the code in Alice. (Recall the Ask a user for a number functions are found in the World’s functions.) For example, look at this possibility:



Now we can test again to see what happens! Hopefully your equation checks out! Good Work!!!!