Calling a Function

Category:Functions

When you define a function you give a name to a set of actions you want the computer to perform. When you call a function you are telling the computer to run that set of actions.

When you call a function you tell the computer to run the code inside the function definition. Functions are called by placing their blocks anywhere in the program.

Examples

Example 1

The function “switchBackgrounds” is used twice in this program. The function can be called instead of rewriting the blocks of code inside of it each time.

Example 2

Functions can be called inside other functions. Here, the function “dropOre” calls the function “moveNorthwest” in it so that the compass exposes the ore after creates one in its location.

Tips

  • The purpose of a function is to help you organize your code and to avoid writing the same code twice. You can you define a function once, and then call the function a number of times.
  • You can call a function within another function.

Found a bug in the documentation? Let us know at documentation@code.org