Routines, conversions and files
Reuse a routine, check an identifier, convert dates, and read a keyed record.
Call a local routine
GOSUB jumps to a label in the same program. The routine shares the program's variables. RETURN resumes execution after GOSUB. STOP ends the main program before it can fall through into the routine.
first_name = 'Mira' last_name = 'Patel' GOSUB make_label CRT label_text STOP make_label: label_text = last_name : ', ' : first_name RETURN
Expected output is Patel, Mira.
Call a routine in another file
CALL invokes a named SUBROUTINE. When you pass a
variable, the routine can assign a result back to it. Save this program as
main.b.
quantity = 3 unit_price = 12 total = 0 CALL calculate_total(quantity, unit_price, total) CRT total
Save the routine as calculate_total.b.
SUBROUTINE calculate_total(quantity, unit_price, total) total = quantity * unit_price RETURN END
Run uvx jbcc --run main.b calculate_total.b from the directory
with both files. Expected output is 36.
Check the shape of an identifier
MATCHES checks the whole string against a jBC pattern.
2A4N means two letters followed by four digits. Try adding
an extra digit to see the other branch.
item_id = 'AB1234' IF item_id MATCHES '2A4N' THEN CRT 'Valid identifier' END ELSE CRT 'Invalid identifier' END
Expected output is Valid identifier.
Work with dates
ICONV turns a written date into an internal day number. Add one to move to the next day, then use OCONV to display it. This example advances to leap day.
day = ICONV('20240228', 'D') next_day = day + 1 CRT OCONV(next_day, 'D')
Expected output is 29 FEB 2024 with English month names.
Write and read a record by key
OPEN gives you a file handle. WRITE stores a dynamic array under a record key and READ retrieves it. ABORT reports error 201 if the file cannot be opened. The READ statement's ELSE branch handles a failed read.
OPEN 'EXAMPLE.CONTACTS' TO contacts ELSE ABORT 201, 'EXAMPLE.CONTACTS' contact = 'Mira Patel' : @FM : 'York' WRITE contact ON contacts, 'C001' ON ERROR CRT 'Could not write C001' STOP END READ saved_contact FROM contacts, 'C001' THEN CRT saved_contact<1> : ' lives in ' : saved_contact<2> END ELSE CRT 'Could not read C001' END
Save the program as contacts.b and run
uvx jbcc --memory-file EXAMPLE.CONTACTS --run contacts.b.
jbcc starts with an empty in-memory file for this run. Expected output
is Mira Patel lives in York.