Run BASIC Functions

Back to help file

ABS(x)
This function returns the absolute value of x.
Usage:
print "The absolute value of -5 is: "; abs(-5)

ACS(x)
This function returns the arc cosine of the number or numeric expression x. The return value is expressed in radians.
Usage:
print "The arc cosine of 0.2 is "; acs(0.2)

ASC(b\$)
This function returns the ASCII value of first character of string b\$.
Usage:
print asc("A")

ASN(x)

This function returns the arc sine of the number or numeric expression x. The return value is expressed in radians.
Usage:
print "The arc sine of 0.2 is "; asn(0.2)

ATN(x)
This function returns the arc tangent of the number or numeric expression x. The return value is expressed in radians.
Usage:
print "The arc tangent of 0.2 is "; atn(0.2)

CHR\$(asciiVal)
This function returns a string of one character which maps to the ASCII value of asciiVal.
Usage:
'print the letters of the alphabet
for x =  65 to 90
print chr\$(x)
next x

COS(x)

This function returns the cosine of the angle x. The angle x should be expressed in radians.
Usage:
for c = 1 to 45
print "The cosine of "; c; " is "; cos(c)
next c

DATE\$() or DATE\$(template\$) or DATE\$(days)
This function returns either a formatted date using template\$ or it returns a number of days in relation to Jan 1, 1901.
Usage:
'using date\$() unmodified
print
"Today is: "; date\$()
'using a custom template
print
"Today in mm/dd/yyyy format: "; date\$("mm/dd/yyyy")
print "Today in mm/dd/yy format: "; date\$("mm/dd/yy")
print "Today in yyyy/mm/dd format: "; date\$("yyyy/mm/dd")
'return a number for today's date
print
date\$("days")
'return a number for April 1, 2002
print
date\$("4/1/2002")
'return a formatted date string for day 35980

print date\$(36980)

DECHEX\$(dec)
This Hexadecimal string for decimal value of dec.  If a non-integer value is used it will be converted to an integer before converting to hexadecimal.
Usage:
print dechex\$(255)

EOF(#handle)
This function returns 1 if open file #handle is at end, else return 0.
Usage:
'open a file and read it all the way through
open "somefile.txt" for input as #1
while eof(#1) = 0
line input #1, text\$
wend
close #1

EXP(x)
This function returns e raised to the power of x where e=2.7182818
Usage:
print exp(1)

HEXDEC(hex\$)
This function returns a decimal number from hex\$.
Usage:
print hexdec("FF")
'an optional &h prefix is permitted
print hexdec("&hFF")
'the letter case doesn't matter
print hexdec("&Hff")

HTTPGET\$(url\$)
Perform an HTTP GET and return the server's response as a string.  A runtime error can occur if the document specified in url\$ doesn't exist, if there is a network connectivity problem, or a timeout, etc.  Always specify an error handler by using ON ERROR GOTO.
Usage:
on error goto [httpGetError]
print "First document fetch..."
doc1\$ = httpget\$("http://www.runbasic.com")
print "Retrieved "; len(doc1\$); " characters."
print "Second document fetch..."
doc2\$ = httpget\$("http://www.someotherbasic.com")
print "You should not get this far!"
print "Retrieved "; len(doc2\$); " characters."
[httpGetError]
print "Error fetching HTTP document."
print "Error description: "; Err\$

HTTPPOST\$(url\$, data\$)
Perform an HTTP POST to the URL specified in url\$ and send the information in data\$.  Return the server's response as a string.  A runtime error can occur if the document specified in url\$ doesn't exist, if there is a network connectivity problem, or a timeout, etc.  Always specify an error handler by using ON ERROR GOTO.

INPUT\$(#handle, count)
This function returns the next count characters from open file #handle.
Usage:
open "textfile.txt" for input as #1
while eof(#1)
char\$ = input\$(#1, 1)
wend
close #1

INPUTTO\$(#handle, delim\$))
This function reads from the file #handle up to the character specified in delim\$ or to the end of the line, whichever comes first. This is handy for reading files which contain comma, tab, pipe, or other delimited items.

'display each comma delimited item in a file on its own line
open "inputto test.txt" for input as #test
while eof(#test) = 0
print inputto\$(#test, ",")
wend
close #test

INSTR(searchThis\$, searchFor\$) or INSTR(searchThis\$, searchFor\$, startingPosition)
This function returns the index of searchFor\$ within searchThis\$.  If searchFor\$ is not contained within searchThis\$ then 0 will be returned.  Specifying the optional startingPosition causes the search to begin at that specified position.
Usage:
'find the first "is"
print instr("this is a test", "is")
'find the second "is"
print instr("this is a test", "is", 4)

INT(number)
This function strips off the fractional part of number and returns only the integer part.
Usage:
for x = 10 to 50 step 2
print int(x/10)
next x

LEFT\$(wholeString\$, count)
This function returns the first count characters from the left end of wholeString\$.
Usage:
text\$ = "This is a test"
for x = 1 to 14
print left\$(text\$, x)
next x

LEN(longString\$)
This function returns a count of characters in longString\$.
Usage:
print len("twenty-six characters long")

LOF(#handle)
This function returns the number of characters/bytes for open file #handle.  It does not work with non-file objects.  It is useful for reading the entire contents of a file with just one read operation.
Usage:
open "sample.txt" for input as #1
count = lof(#1)
close #1
print "Size of file: "; count

LOG(x)
This function returns the natural logarithm of x.
Usage:
print log(10)
print log(0.1)

LOWER\$(someString\$)
This function returns a lowercased version of someString\$.
Usage:
print lower\$("AbCdEfGhIjK")

MAX(x, y)
This function returns x or y, whichever is greater.  If they are equal it will return that mutual value.
Usage:
print max(3, 5); " is greater."

MID\$(someString\$, startingPosition) or MID\$(someString\$, startingPosition , count)
This function returns a string copied from someString\$ starting at startingPosition to the end of someString\$.  If the optional count parameter is used then it copies that many characters unless count represents more characters then exist between startingPosition and the end of someString\$.  In this case it will copy from startingPosition to the end of someString\$.
Usage:
'both of these examples print the word 'test'
print mid\$("this is a test", 11)
print mid\$("let's test this", 7, 4)

Note: If startingPosition is a value less than 1 then copying will begin at the first character.  If startingPosition is greater than the size of someString\$ then an empty string will be returned.

MIN(x, y)
This function returns x or y, whichever is lesser.  If they are equal it will return that mutual value.
Usage:
print min(3, 5); " is lesser."

MKDIR(path\$)
Create directory specified in path\$. Return 1 for success or 0 for failure.  If the directory already exists, that will also cause failure to occur.
Usage:
input "Type a directory to create"; path\$
succeeded = mkdir(path\$)
if succeeded = 1 then print "Success!"

RIGHT\$(someString\$, count)
This function returns the last count characters from someString.  If there are fewer characters in someString\$ than specified by count, return the whole string.  If zero or less characters are specified by count, return an empty string.
Usage:
print "Returned "; right\$("one two three four", 4); " characters. ;-)"

RMDIR(path\$)
Delete the directory specified by path\$.  Return 1 for success or 0 for failure.  If the directory specified doesn't exist in the first place, this also causes failure to occur.
Usage:
input "Type a directory to remove"; path\$
succeeded = rmdir(path\$)
if succeeded = 1 then print "Success!"

RND(n)
This function returns a pseudorandom value between 0 and 1.  The n parameter doesn't do anything, but it's there for syntax compatibility with other BASIC languages.  There is no need to seed the random number generator each time the program is run to produce different results.
Usage:
print "Here is a random number between one and ten: "; int(rnd(1)*10)+1

SHELL\$(command\$)
Take the content of command\$ and feed it to a command line shell for execution.  Return the resulting text output as a string (an empty string if there is no output).
Usage:
'for Windows
print shell\$("dir *.*")

'for Linux/Mac OS X
print shell\$("ls -la")

Note: Windows seems to have no trouble launching GUI based programs using this approach (notepad for example), but Linux/Mac OS X systems return an error if the program executed does anything fancier than stdio.

SIN(x)
This function returns the sine of the angle x. The angle x should be expressed in radians.
Usage:
for x = 1 to 45
print "The sine of "; x; " is "; sin(x)
next x

SPACE\$(count)
This function returns a string containing count space characters.
Usage:
print "There are 10 space characters between here"; space\$(10); "and here."

SQR(x)
This function returns the square root of x.  Attempting to compute the square root of a negative number causes a runtime error.
Usage:
print "Hypotenuse computer"
input "Input length of side a"; a
input "Input length of side b"; b
print "Length of side c is: "; sqr(a^2+b^2)

STR\$(x)
This function returns a string representation of number x
Usage:
stringOfNumber\$ = str\$(22/7)
print stringOfNumber\$; " is "; len(stringOfNumber\$); " characters long."

TAN(x)
This function returns the tangent of the angle x. The angle x should be expressed in radians.
Usage:
for x = 1 to 45
print "The tangent of "; x; " is "; tan(x)
next x

TIME\$() or TIME\$(modifier\$)
This function returns the current time as a formatted string.  If the optional modifier\$ parameter is present the function returns a numeric value according to the example below.
Usage:
print time\$() 'time now as string "16:21:44"
print time\$("seconds") 'seconds since midnight as number 32314
print time\$("milliseconds") 'milliseconds since midnight as number 33221342
print time\$("ms") 'milliseconds since midnight as number 33221342

TRIM\$(someString\$)
This function returns a copy of someString\$ with leading and trailing spaces removed.
Usage:
print "*"; trim\$("         spaces removed       "); "*"

UPPER\$(someString\$)
This function returns a copy of someString\$ with all letters converted to uppercase.
Usage:
print upper\$("AbCdEfGhIjK")

USING(template\$, x)
This function returns a formatted string for x using template\$, for example "####.##".  Commas are permitted in the template.  If the template does not have enough places to display the integer portion of x the entire integer portion will be displayed and a % will be added to the beginning to signify formatting overflow.
Usage:
value = 1234.567
print "----------"
print using(" ####.#", value)
print using(" #,###.##", value)
print using("##.##", value)   'formatting overflow

VAL(someString\$)
This function returns a numeric value from someString\$.  Conversion of scientific notation is also supported.
Usage:
print val("3.14")
print val("314e-2")
print val("0.0314e2")

WORD\$(someString\$, n) or WORD\$(someString\$, n, delimiter\$)
This function returns the nth word from someString\$ using whitespace as delimiter.  If the optional delimeter\$ parameter is specified , then words will be broken up using that delimiter instead of whitespaces.
Usage:
print word\$("the chosen word", 2)
print word\$("delimited,using,commas", 3, ",")