Concept Explanation: Elements of a Function

Printer-friendly version

RobotC is considered a high-level language. This grants us access to a large library of functions to perform actions with. These functions contain a large number of instructions that translate our commands to something the NXT can understand. It is important to realize that the NXT doesn't understand words and numbers like we do. Programming for an NXT is like talking in another language. The rules of this language is called syntax. Each element has specific rules that allow the NXT to understand what you want it to do.

To understand the syntax of functions, let's take a look at the functions we used in the example.

-- wait1Msec --
  • Function Syntax: wait1Msec( [int] time );

  • Usage: Put task in idle state for a number (parameter1) of milliseconds.

We look at this function first because it is the most simple function that has all four elements of a function. Let's look at each element in turn.

  1. Function Name - This name allows the RobotC compiler look in its' library of functions and add the correct commands to the compiled program.
  2. Parameter - This Data controls the function. Wait1Msec requires an integer from 1 to 32,767. What this data does depends on the function that you are using. The number of parameters vary depending on the function.
  3. Encapsulation - Marks the start and stop of the parameter(s). All functions must have the encapsulation parentheses, even when there are no parameters.
  4. End Marker - The semicolon marks the end of the command. Because while space is ignored, semicolons are used to separate instructions.
Concepts: