NQC Program Structure n 3 types of code

  • Slides: 8
Download presentation
NQC Program Structure n 3 types of code blocks with their own features and

NQC Program Structure n 3 types of code blocks with their own features and limitations u 1. Tasks u 2. Subroutines u 3. Inline Functions

Tasks n Syntax task name ( ) { //task body } Example n n

Tasks n Syntax task name ( ) { //task body } Example n n n Program must have task named “main” Program may have up to 10 tasks Implement tasks using “start” & “stop” (except “main” task)

Subroutines n n Syntax sub name ( ) { //subroutine body } Single copy

Subroutines n n Syntax sub name ( ) { //subroutine body } Single copy of code is shared by different callers (efficient) n n n Cannot use arguments Cannot call other subs 8 subs maximum No local variablescalled from multiple tasks Mostly used when code size is limited

Inline Functions Syntax void name (arguments) { //Function body } n Leads to excessive

Inline Functions Syntax void name (arguments) { //Function body } n Leads to excessive code size (each call adds copy) n n n “return” causes function to return Arguments u Empty, one, more u Correct number & type u Defined by “type” followed by “name” u example

Argument types TYPE MEANING LIMITATION int Pass by value none const int Pass by

Argument types TYPE MEANING LIMITATION int Pass by value none const int Pass by value Constants only int& Pass by reference Variables only const int& Pass by reference Cannot modify argument

Variables n n n 16 bit signed integers Max 32 variables Declared & initialized

Variables n n n 16 bit signed integers Max 32 variables Declared & initialized same as in Java Global variables declared outside block Local variables accessible inside block where defined n Example: int x; // global task main ( ) { int y; // local to main {// compound stment int z; // local to cmpd y = z; // valid asgmnt } y = z; // error- z not in scope

Sensors n n n SENSOR_1, 2, or 3 identifies RCX sensor ports You can

Sensors n n n SENSOR_1, 2, or 3 identifies RCX sensor ports You can set the type and the mode for each sensor For convenience just use Set. Sensor Examples: n Set. Sensor. Type (SENSOR_1, SENSOR_TYPE_LIGHT); n Set. Sensor. Mode(SENSOR_1, SENSOR_MODE_PERCENT); n Set. Sensor(SENSOR_1, SENSOR_LIGHT);

Outputs n n Out_A, B, or C identifies output ports 3 attributes: u Mode

Outputs n n Out_A, B, or C identifies output ports 3 attributes: u Mode t On, Off , Float u Direction t Fwd, Rev, Toggle u Power level t Set. Power(Out_A, 7) n Examples: u On. Fwd(Out_A); u Rev (Out_B); u Off(Out_A+Out_B); u On. For(Out_C, 100); MISC Wait(time); time = 100 ths sec