Miscellaneous functions
SRON provides these 15 functions :
beep()
copy()
exit()
get_exec_time()
len()
randint()
randdouble()
randrange()
setprecision()
sizeof()
sleep()
sron_cmd()
syscmd()
throw_exception()
typeof()
version()
1. beep():
This function is used to make a beep sound. It requires two 'Int' type arguments frequency and duration.
{
name : Main
@ the beep sound is made for 1000 milliseconds(1 second)
beep(500, 1000)
}
2. copy():
This function creates a copy of the value passed to it. So, the changes made to the original value does not affect the new value.
{
name : Main
String a = "abc"
String b = copy(a)
push(a, 'd')
println(a, '\n' , b)
}
OUTPUT:
abcd abc
3. exit():
This function exits the program and suddenly stops it. It takes a 'Int' value as argument.
exit(exit_code)
This exit_code tells the operating system, why program halted.
If exit_code is 0, program is exiting due to no errors. If exit_code is 1, program is exiting due to some minor errors. If exit_code is -1, program is exiting due to some serious errors.
{
name : Main
exit(0)
}
4. get_exec_time():
This function returns the time taken by program to execute the code.
{
name : Main
Int a = 0
for : {
range : (Int i=1, 1000)
a += i
}
println("Time Taken = ", get_exec_time(), " seconds.")
}
OUTPUT:
Time Taken = 0.0001 seconds
5. len():
This function returns the length of any value passed.
{
name : Main
@ length of a 'String' value
length = len("ABCDEFGH")
println(length)
@ length of a 'List' value
length = len([1,2,3,4,5,6,7,8,9,0])
println(length)
}
OUTPUT:
8 10
6. randint():
This function returns a random 'Int' type value. Takes no arguments.
{
name : Main
Int random_num = randint()
println(random_num)
}
OUTPUT:
5251570147017362282
7. randdouble():
This function returns a random 'Double' type value. Takes no arguments.
{
name : Main
Double random_num = randdouble()
println(random_num)
}
OUTPUT:
0.947283
8. randrange():
This function returns a random number between a particular range. It takes two arguments start_point and end_point. If you pass two 'Int' values, then it will generate a 'Int' type random value between them. And if you pass two 'Double' values, then it will generate a random 'Double' type value between them.
{
name : Main
Int random_num = randrange(0,10)
println(random_num)
random_num = randrange(0.0, 10.0)
println(random_num)
}
OUTPUT:
6 9.89077
9. setprecision():
This function sets the number of decimal digits to be printed by 'print()' and 'println()' function. setprecision(num_of_precision) If num_of_precision must be more than 0 and less than 10, otherwise by default 5 decimal digits will be set as the precision.
{
name : Main
Double num = 123.123456789
println(num)
setprecision(3)
println(num)
}
OUTPUT:
123.123457 123.123
10. sizeof():
This function returns the size of the data in bytes. You can use it to find how much memory, your variable is taking.
{
name : Main
@ getting the size of 'Int' value...
Int size = sizeof(12345)
println(size)
@ getting the size of 'Double' value...
size = sizeof(12345.64244)
println(size)
@ getting the size of 'Char' value...
size = sizeof('A')
println(size)
@ getting the size of 'Bool' value...
size = sizeof(false)
println(size)
@ getting the size of 'String' value...
size = sizeof("Hello, SRON!")
println(size)
@ getting the size of 'List' value...
size = sizeof([1,2,3,4,5,6])
println(size)
}
OUTPUT:
8 8 1 1 32 48
11. sleep():
This function is used to pause the execution for some given time. It expects an integer type value as argument. It pauses the code for given milliseconds.
{
name : Main
println("Sleeping")
sleep(1000)
@ the code will be paused for 1 seconds (1000 milliseconds)
println("Waking")
}
12. sron_cmd():
This function can be used to get information about internal components of SRON's interpreter and perform operations on them too. Know more from here.
{
name : Main
OBUFFER += "Hello there! Saksham Joshi"
sron_cmd(SRON_CMD_OBUFFER_CLEAR)
console : ( '>' , OBUFFER , '<') @ Output: ><
}
13. syscmd():
This function is used to execute a command in the host operating system's command processor. It takes a 'String' type value as argument. If the command is executed, then it returns a 'Void' value otherwise displays an exception.
{
name : Main
syscmd("sron-docs")
@ official docs of Sron will open in your browser
}
14. typeof():
This function returns a 'String' value specifying the type of the passed value.
{
name : Main
println(typeof(1234))
println(typeof(1234.9973))
println(typeof('S'))
println(typeof(false))
println(typeof("SRON"))
println(typeof([1,2,3,4]))
}
OUTPUT:
Int Double Char Bool String List
15. throw_exception()
This function is used to display error message during runtime. It takes a String type input.
{
name : Main
List lst = [1,2,3,4,5]
Int inp = input_int("Enter the index :- ")
if : {
condition : ~{ inp < 0 || inp >= len(lst) }~
throw_exception(IndexNotWithinRangeException)
}
println(at(lst, inp))
}
16. version():
This function returns the version of SRON installed in your system.
{
name : Main
println(version())
}
OUTPUT:
2.2
Last updated