BFOIT - Introduction to Computer Programming

Appendix C (Primitive Procedures)

Introduction

jLogo's primitive procedures are core building blocks of the language. You may not use the names of of these primitive procedures for names of procedures you write.

Data Manipulation

Name Input(s) Description Example
BUTFIRST
BF
wordOrSentence If the input is a word, all characters except its first are output. If the input is a sentence, all words except the first are output. BUTFIRST :WD
BUTLAST
BL
wordOrSentence If the input is a word, all characters except its last are output. If the input is a sentence, all words except the last one are output. BUTLAST :SENT
CHAR number outputs a single character word that is the ASCII code corresponding to the input (an integer between 0 and 127). CHAR 65
COUNT array
number
wordOrSentence
If the input is an array, the number of members it is composed of is output.  If the input is a number or a word, the number of characters in it is output.  If the input is a sentence, the number of words in it is output. COUNT :WD
EMPTYP
EMPTY?
wordOrSentence Outputs true if its input is a word that has no characters in it, otherwise false. Outputs true if its input is a sentence that has no words in it, otherwise false. EMPTY? :SENT
FIRST wordOrSentence If the input is a word, its first character is output. If the input is a sentence, its first word is output. FIRST :WD
ITEM number
wordOrSentence
Outputs the number-th element of its second input.  If its second input is a word, it outputs a word consisting on the number-th character. If its second input is a sentence, it outputs the number-th word of it. ITEM 2 :SENT
LAST wordOrSentence If the input is a word, its last character is output. If the input is a sentence, its last word is output. LAST :WD
MEMBERP
MEMBER?
characterOrWord
wordOrSentence
Outputs true if its first input is in its second input, otherwise it outputs false. MEMBER? "e :WD
 READLIST  Get a line of text input from the user (in the CommandCenter) and output it as a sentence.  MAKE "sent READLIST
 READWORD  Get a line of text input from the user (in the CommandCenter) and output it as a word.  MAKE "yesNo READWORD
SE
SENTENCE
wordOrSentence
wordOrSentence
Outputs a sentence consisting of its first input concatenated with its second input. SENTENCE "word :sent
WORD word1
word2
Outputs a word consisting of its first input concatenated with its second input. WORD "$ :word

Flow-Control Procedures

Name Input(s) Description Example
IF trueFalse
instructionList
If the trueFalse input is true, the instructionList input is performed.  If trueFalse is false, nothing is done. IF LESS? MOUSEX 0 [ ... ]
IFELSE trueFalse
instructionList1
instructionList2
If the trueFalse input is true, the instructionList1 input is performed.  If trueFalse is false, instructionList2 is performed. IFELSE PENDOWN? [ ... ] [ ... ]
OUTPUT value The current invocation of the procedure in which OUTPUT exists terminates and the value it outputs is value. OUTPUT RANDOM 16
REPEAT count
instructionList
The instructionList input is performed the number of times specified by the count input. REPEAT 4 [ FD 100 RT 90 ]
REPEAT rangeSentence
instructionList
The instructionList input is performed while REPCOUNT is within the range of the first and second values in rangeSentence; REPCOUNT starts at the first value in rangeSentence and moves toward the second value in rangeSentence in increments as specified by the third value in rangeSentence. REPEAT [-2 2 .5] [PR REPCOUNT]
 RUN   sentence Execute the sentence as if it were typed into the CommandCenter - instructions as data.  RUN  :cmdsSent
STOP   The current invocation of the procedure in which STOP exists terminates. IF EQUAL? :inp 0 [STOP]
WAIT number Performing instructions is suspended for the number of milliseconds (1/1000s of a second). WAIT 1000

Graphics Procedures

Name Input(s) Description Example
BACK
BK
number Moves the turtle backward, i.e., exactly opposite to the direction that it's facing, by the specified number of turtle steps. BACK 150
BG number Sets the color of the background of the GraphicsCanvas.  Color is expressed as a number.
Number
Color
Number
Color
0
black
8
brown
1
blue
9
tan
2
green
10
forest
3
cyan
11
aqua
4
red
12
salmon
5
magenta
13
violet
6
yellow
14
orange
7
white
15
grey
BG 0
CG
CLEAN
  Erases everything that the turtle has drawn on the graphics window.  The turtle's state (position, heading, pen color, etc...) is not changed.  
COLORUNDER   The color under the turtle, at the tip of the pen, is output. PRINTLN COLORUNDER
FILL   Whatever color is under the turtle is changed to the turtle's color, and this process is repeated in an outward manner for all pixels that are neighbors and match the original color. FILL
FORWARD
FD
number Moves the turtle forward, in the direction it is facing, by the specified number of turtle steps. FD 100
HEADING   Outputs the turtle's heading in degrees.  
HIDETURTLE
HT
  Hides the turtle, makes it invisible.  
HOME   Moves the turtle to the center of the graphics window, i.e., coordinates 0,0.  
LABEL wordOrSentence The text representing LABEL's input is drawn in the graphics window. LABEL "START
LEFT
LT
number Turns the turtle counterclockwise by the specified angle measured by a number of degrees (1/360 of a circle). LEFT 180
 LOADPICTURE 
 LOADPICT
 word Load the .BMP picture specified by word into the background of TG's graphics canvas.  LOADPICT  "natalie
 LOADSHAPE  word  number  Load the .BMP picture named word into TG's turtle shapes; the picture is given the identifier number. Number must be in the range of 16 through 31. The SETSHAPE command can then be used to set the turtle's image to the picture.  LOADSHAPE  "bat 16 
PENCOLOR   Outputs the color number of the turtle's pen. MAKE "savCol PENCOLOR
PENDOWN
PD
  Puts the turtle's pen down so that it leaves a trace when it moves.  
PENDOWN?   Outputs true if the turtle's pen is in the down position, otherwise false.  
PENSIZE   Outputs the size of the turtle's pen, its width in turtleSteps. MAKE "savSiz PENSIZE
PENUP
PU
  Lifts the turtle's pen up so that it leaves no trace when it moves.  
POS   Outputs the current coordinates of the turtle as a sentence.  The X coordinate is the FIRST of the sentence; the Y coordinate is the LAST of the sentence. MAKE "point POS
RIGHT
RT
number Turns the turtle clockwise by the specified angle, measured in degrees (1/360 of a circle). RT 90
SETHEADING
SETH
number Turns the turtle to a new absolute heading. The input, number, is the heading (in degrees) clockwise from the positive Y axis, i.e.,
0 is due North (up),
90 is due East (right),
180 is due South (down), and
270 is due West (left).
SETH 90
SETLABELHEIGHT
SETLH
number The height of characters drawn with the LABEL procedure is set to the input.
  Number  
 CharHeight 
 CharWidth 
12
9
7
14
10
8
16
12
9
18
14
10
24
18
14
32
22
18
SETLABELHEIGHT 32
SETPENCOLOR
SETPC
number Sets the turtle's color - the color of its pen.  Color is expressed as a number.
Number
Color
Number
Color
0
black
8
brown
1
blue
9
tan
2
green
10
forest
3
cyan
11
aqua
4
red
12
salmon
5
magenta
13
violet
6
yellow
14
orange
7
white
15
grey
SETPC 10
SETPENSIZE
SETPS
number Sets the width of the turtle's pen, which determines the thickness of the trace it leaves, the line it draws. SETPS 5
SETPOS sentence Moves the turtle to an absolute position, specified by a sentence containing two numbers.  The two numbers are (FIRST) a new horizontal (X) coordinate and (LAST) a new vertical (Y) coordinate. SETPOS [100 100]
SETSHAPE
SETSH
number Sets the turtle's shape to a specified shape.  Shape is expressed as a number.  The built-in shapes are:
Number
Shape
Number
Shape
0
Turtle
3
Box
1
Arrow
4
Cross
2
Ball
5
Triangle
SETSHAPE 2
SETSHAPE
SETSH
number-sentence Sets the turtle's shape to a specified shape and size.  The first number in the sentence is the shape number (see SETSHAPE number) and subsequent numbers specify a preferred size, which is shape dependent. SETSHAPE [3 100 10]
SETX number Moves the turtle horizontally to the specified absolute window position. SETX 100
SETXY number1
number2
Moves the turtle to an absolute position, specified as two numbers: the new horizontal (X) and vertical (Y) coordinates. SETXY 50 -30
SETY number Moves the turtle vertically to the specified absolute window position. SETY 100
SHOWTURTLE
ST
  Makes the turtle visible.  
XCOR   Outputs the turtle's X-coordinate.  
YCOR   Outputs the turtle's Y-coordinate.  

Logical Procedures

Name Input(s) Description Example
AND trueFalse1  trueFalse2 Outputs true if trueFalse1 AND trueFalse2 are true, false otherwise.
td>  trueFalse1 
 trueFalse2   Output 
true true true
true false false
false true false
false false false
AND true true
NOT trueFalse Outputs false if trueFalse is true, else true if trueFalse is false - the opposite trueFalse value. NOT true
OR trueFalse1  trueFalse2 Outputs true if trueFalse1 OR trueFalse2 is true, false otherwise.
 trueFalse1   trueFalse2   Output 
true true true
true false true
false true true
false false false
OR false false

Math Procedures

Name Input(s) Description Example
ABS number Outputs the absolute value of number ABS -15
ASCII character Outputs the integer (between 0 and 127) that represents the character in the ASCII code. ASCII "A
CEIL number Outputs the smallest integer (closest to negative infinity) that is not less than number. CEIL 44.223
COS number Outputs the trigonometric cosine of number which is in degrees COS 45
DIFFERENCE number1  number2 Outputs the result of subtracting number2 from number1 DIFFERENCE 15 7
FLOOR number Outputs the largest integer (closest to positive infinity) that is not greater than number. FLOOR 12.875
MINUS number Outputs the negative of number MINUS 122
PRODUCT number1  number2 Outputs the result of multiplying number1 by number2 PRODUCT 10 6
QUOTIENT number1  number2 Outputs the result of dividing number1 by number2 QUOTIENT 12 4
RANDOM number Outputs some integer greater-than or equal-to zero AND less-than number RANDOM 100
REMAINDER number1  number2 Outputs the remainder left after dividing number1 by number2 REMAINDER 17 3
ROUND number Outputs the closest integer to number ROUND 22.45
SIN number Outputs the trigonometric sine of number which is in degrees SIN 45
SQRT number Outputs the square root of number SQRT 16
SUM number1  number2 Outputs the result of adding number1 and number2 SUM 3 4
TAN number Outputs the trigonometric tangent of number which is in degrees  

Multi-Threading Support

Name Input(s) Description Example
 NEWTURTLE  name Creates a new turtle with the specified name.  The new turtle appears in the home position with its pen down. NEWTURTLE "t2
 TELL name
instructionList
Tells the turtle named name to perform the instructionList. TELL "t2 [ FD 10 ]

Predicates

Name Input(s) Description Example
EQUAL?
EQUALP
value1
value2
If value1 is equal to value2, EQUAL? outputs true.  If not, false is output. EQUAL? MOUSEX 0
GREATER?
GREATERP
value1
value2
If value1 is greater than value2, GREATER? outputs true.  If not, false is output. GREATER? MOUSEX 0
LESS?
LESSP
value1
value2
If value1 is less than value2, EQUAL? outputs true.  If not, false is output. LESS? MOUSEX 0

User-Interface Procedures

Name Input(s) Description Example
CANVASHEIGHT   Outputs the current height of the graphics window in turtle steps.  
CANVASWIDTH   Outputs the current width of the graphics window in turtle steps.  
KEYPRESSED keyNumber When a keyboard key is pressed, TG receives an event.  If the key is one that TG is interested in, a user-defined procedure with the name KEYPRESSED (expecting one input) is invoked if it has been defined.
 Key Number   Keyboard Key 
10 Enter
32 Space
48 - 57 0 - 9
65 - 90 A - Z
97 - 122 a - z
128 Down-Arrow
129 Left-Arrow
130 Right-Arrow
131 Up-Arrow
TO keyPressed :num
  PRINTLN WORD "key= :num 
  END
MOUSECLICKED   When a mouseClicked Event is received by the jLogo interpreter, it performs a user-defined procedure with the name MOUSECLICKED, if one has been defined.  
MOUSEMOVED   When a mouseMoved Event is received by TG, it performs a user-defined procedure named mouseMoved, if one has been defined.  mouseMoved Events only happen when the graphics canvas is active, i.e., the mouse was previously clicked on it.  to mouseMoved   
   ...
   end
MOUSEX   Outputs the X-coordinate where the mouse was when it was clicked.  
MOUSEY   Outputs the Y-coordinate where the mouse was when it was clicked.  
PRINT thing Displays thing in the terminal window. PRINT MOUSEX
PR
PRINTLN
thing Displays thing in the terminal window.  The line is then finished off by moving the cursor to the start of the next line. PRINTLN HEADING

Variables

Name Input(s) Description Example
ARRAY size Outputs an array of size members, each of which is initially an empty sentence.  Size must be a positive integer. MAKE "ARY ARRAY 5
GLOBAL name Declares a global variable named name.  The variable can be referenced anywhere in the program's text following this command.  The variable has no initial contents. GLOBAL "var
LOCAL name Declares a local variable named name.  The variable is local to the procedure it is in.  The variable has no initial contents. LOCAL "var
MAKE name
value
Creates a variable named name if it doesn't already exist.  The contents of the variable is set to value. MAKE "WHITE 7
THING name Outputs the value in its input, a global variable named name.  ":NAME" is an abbreviation for THING "NAME.  But, its real need is when the names of global variables are constructed dynamically. THING :curSequence
SETITEM index
array
value
Replaces the indexth member of array with value. SETITEM 2 :nums "two


Back to TG Directives
Go to the Table of Contents
On to TG Editor