PencilCoder

Return!

Goals:

  • Write functions that compute values or create objects.

Coding Snippets:

  • coding snippets
  • Use a return statement to instruct a function to return a data value back to the place in your code where the function was called.
  • A function can return a value of any data type, including number, string, arrays, objects (including labels and sprites), and even other functions.

Math and Computer Concepts:

  • A return statement exits a function; any code in the function that appears after it will not be executed. This is a useful feature when using conditional logic, as illustrated by the isPrime function shown to the right. If the argument passed to the function is less than 2, isPrime exits with the execution of return false in the first if block. The last statement, return true, is only executed if none of the three return statements above it are executed.
  • A return statement can only be used to pass a single value. While this may seem like a significant limitation, it is easy to work around using a collection of data, i.e., an array or an object. getxy and pagexy are examples of built-in functions that return arrays; the expression random(position) returns an object.

Activities:

Save each of the following in a folder called .