Miscellaneous functions

SRON provides these 15 functions :

  • beep()

  • copy()

  • display_exception()

  • exit()

  • get_exec_time()

  • len()

  • randint()

  • randdouble()

  • randrange()

  • setprecision()

  • sizeof()

  • sleep()

  • syscmd()

  • 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. display_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) }~
        display_exception(IndexNotWithinRange)
    }

    println(at(lst, inp))
}

4. 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)
}

5. 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


6. 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


7. randint():

This function returns a random 'Int' type value. Takes no arguments.

{
    name : MAIN
    
     Int random_num = randint()
     println(random_num)
}

OUTPUT:

5251570147017362282


8. randdouble():

This function returns a random 'Double' type value. Takes no arguments.

{
    name : MAIN
    
     Double random_num = randdouble()
     println(random_num)
}

OUTPUT:

0.947283


9. 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


10. 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(5)
     println(num)
}

OUTPUT:

123.123457 123.12346


11. 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


12. 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")
}

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. version():

This function returns the version of SRON installed in your system.

{
    name : MAIN
    println(version())
}

OUTPUT:

2.1


Last updated