Fall 2007ACS-1805 Ron McFadyen1 Chapter 6 Functions & If/Else
Fall 2007ACS-1805 Ron McFadyen2 Functions & If/Else A function is similar to a method in that it has a name, instructions, and possibly parameters, but differs in that a function returns a value (to be used from where it was called). An If/Else is a decision control structure (a special type of instruction) that results in exactly one, out of two groups of instructions, to be executed. Which group is executed is determined by the value of an expression (that can involve the computation of a function).
Fall 2007ACS-1805 Ron McFadyen3 Functions Alice provides built-in functions, and allows you to write your own
Fall 2007ACS-1805 Ron McFadyen4 Functions Suppose we want the alienOnWheels to move to the rock. Suppose the distance to move is the distance separating the two less one metre. Consider a move instruction that utilizes the value returned by the distance to function Function returns a value Expression (value passed to move)
Fall 2007ACS-1805 Ron McFadyen5 Writing Your Own Functions Notice the available statements Return statement
Fall 2007ACS-1805 Ron McFadyen6 If / Else The effect of an If/Else is often illustrated with the following diagram conditio n Action AAction B Depending on the value of condition, exactly one of Action A or Action B is executed truefalse
Fall 2007ACS-1805 Ron McFadyen7 Nested If / Else The effect of an If/Else is often illustrated with the following diagram conditio n Action AAction B Depending on the value of condition, exactly one of Action A or Action B is executed truefalse
Fall 2007ACS-1805 Ron McFadyen8 Tips/Techniques Random Numbers