140 likes | 178 Views
Learn about functions and expressions in Alice, including built-in functions and real-life applications. Explore how to utilize functions and math operations to enhance simulations. Follow step-by-step examples to enhance your skills in Alice programming.
E N D
What will we learn today? • We will learn today • What are functions and expressions • Functions in real life • Built-in functions in Alice • How to use functions and expressions
Functions and Expressions • Function • A statement you can use to ask a question about conditions/properties of an object or to compute a value • Expression • A math operation on numbers or other kinds of values
Functions in Real-Life • We use functions to compute the hours in any given number of days (hours=days*24) • The vending machine; you insert a currency into the machine and it dispenses items such as snacks, chocolates, water..etc to you automatically
Functions • We use functions to get the information we need • Q:How far away the camel is from the palm trees??
Built-in Functions • Alice system provides a set of built-in functions for each object • To view a list of built-in functions associate with an object in a world • Select the object in the object tree • View the functions in the details area
Built-in Functions • The Built-in functions are divided into subcategories • Proximity: how close the object is to other objects e.g. distance to • Size: dimensions e.g. height, width and depth • Spatial relation: orientation compared to other objects in the world e.g. to right of • Point of view: position in the world • Other: miscellaneous items
Expressions • Alice Provides math operators for common math expressions • Add(+), subtract(-), multiply (*) and divide(/) • Obtaining math expression
Camel and distance to tree example • We want the camel to move towards the palm tree and stops there • Select the camel from the object tree • Click on the methods tab • Drag the ‘move’ method and place it into the program code editor (the large yellow pane)
Camel and distance to tree example • Choose forward and 1m from the list shown • Select the functions tab and drag the ‘distance to’ function under ‘proximity’ into the ‘1 meter’ box
Camel and distance to tree example • Choose ‘plamTree’ from the list and the ‘entire plamTree’
Camel and distance to tree example • Click on the arrow as shown below and choose math, camel distance to palmTree-1
Let’s work with Alice • We will take the “raceCar” simulation and make the car • Move to the palmTree • Turn to face the lampPost • Move forward half the distance to the lampPost