180 likes | 346 Views
Built-in Functions & Arithmetic Expressions. Recall :. A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance .
E N D
Built-in Functions & Arithmetic Expressions
Recall: • A curupira is playing soccer in the rainforest. The curupira has a soccer ball and will kick the soccer ball toward a pond in the forest. Each kick causes the soccer ball to move forward a random distance. • If the curupira gets the soccer ball in the water, the curupiradoes a dance, or else he disappears in disgust
Where we left off Working on running to the soccer ball Do in order curupira runs to soccer ball curupira kicks the soccer ball a random distance If soccer ball is in the water, curupira dances else curupira disappears
Recall from previous session… Curupira moves forward, but how far? How can we be sure that 3 meters is the necessary distance?
Functions • A function asks a question (to check a condition or compute a value). • In Alice, a function is used to get the information we need concerning • the properties of objects • Is the water blue? • the relationship of one object to another • What is the distance between the curupira and the soccer ball?
Alice 3 Methods Panel Performs an action; does NOT compute and return a value Computes and returns a value; does NOT perform an action
Return a value of a specific data type Built-in functional methods Property values Decimal Number Boolean String
Video: Calling a function • This video illustrates slide 10
Calling a functional method We use the getDistanceTofunction to determine the distance between the curupira and the targetobject
Embedded within a statement A call to a function is embedded within a Example: Calling getDistanceTo The return value (distance value returned by the function)is used as the value for distance in the move statement
Why? • The problem is that distance between two objects is measured center-to-center. • One way to solve this problem is to subtract a small number from the distance.
Arithmetic Expressions Traditional math and formula computations are implemented as arithmetic expressions in a computer program. An arithmetic expression is a combination of operators and operands evaluates to a numeric value
A numeric operand can be either • A whole number (integer) • Or a floating pt number (decimal number).
Arithmetic operators are symbols that can easily be typed on a keyboard Addition + Subtraction - Multiplication * Division / Remainder (mod) %
Video: 18. Building Arithmetic Expressions • This video illustrates slides 18-20
Arithmetic expressions in Alice Click pull-down menu arrow for getDistanceToand select Math Then, build an expression from the cascading menus.
Binary operation Cascading menus assume a binary operation Operand Operator Operand Resulting statement: