Author: Saim Khalid

  • Data Types

    Kotlin data type is a classification of data which tells the compiler how the programmer intends to use the data. For example, Kotlin data could be numeric, string, boolean etc.

    Kotlin treats everything as an object which means that we can call member functions and properties on any variable.

    Kotlin is a statically typed language, which means that the data type of every expression should be known at compile time.

    Kotlin built in data type can be categorized as follows:

    • Number
    • Character
    • String
    • Boolean
    • Array

    (a) Kotlin Number Data Types

    Kotlin number data types are used to define variables which hold numeric values and they are divided into two groups: (a) Integer types store whole numbers, positive or negative (b) Floating point types represent numbers with a fractional part, containing one or more decimals.

    Following table list down all the Kotlin number data types, keywords to define their variable types, size of the memory taken by the variables, and a value range which can be stored in those variables.

    Data TypeSize (bits)Data Range
    Byte8 bit-128 to 127
    Short16 bit-32768 to 32767
    Int32 bit-2,147,483,648 to 2,147,483,647
    Long64 bit-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
    Float32 bit1.40129846432481707e-45 to 3.40282346638528860e+38
    Double64 bit4.94065645841246544e-324 to 1.79769313486231570e+308

    If we will try to store a value more than permitted value in a variable of particular data type, the Kotlin compiler will complain because an overflow would occur at runtime.

    Example

    Following example shows how to define and access different Kotlin number data types:

    funmain(args: Array<String>){val a: Int =10000val d: Double =100.00val f: Float =100.00fval l: Long =1000000004val s: Short =10val b: Byte =1println("Int Value is "+ a)println("Double  Value is "+ d)println("Float Value is "+ f)println("Long Value is "+ l )println("Short Value is "+ s)println("Byte Value is "+ b)}

    When you run the above Kotlin program, it will generate the following output:

    Int Value is 10000
    Double  Value is 100.0
    Float Value is 100.0
    Long Value is 1000000004
    Short Value is 10
    Byte Value is 1
    

    (b) Kotlin Character Data Type

    Kotlin character data type is used to store a single character and they are represented by the type Char keyword. A Char value must be surrounded by single quotes, like ‘A’ or ‘1’.

    Example

    Following example shows how to define and access a Kotlin Char data type:

    funmain(args: Array<String>){val letter: Char    // defining a Char variable
       letter ='A'// Assigning a value to itprintln("$letter")}

    When you run the above Kotlin program, it will generate the following output:

    A
    

    Kotlin supports a number of escape sequences of characters. When a character is preceded by a backslash (\), it is called an escape sequence and it has a special meaning to the compiler. For example, \n in the following statement is a valid character and it is called a new line character

    println('\n')//prints a newline characterprintln('\$')//prints a dollar $ characterprintln('\\')//prints a back slash \ character

    The following escape sequences are supported in Kotlin: \t, \b, \n, \r, \’, \”, \\ and \$.

    (c) Kotlin String Data Type

    The String data type is used to store a sequence of characters. String values must be surrounded by double quotes (” “) or triple quote (“”” “””).

    We have two kinds of string available in Kotlin – one is called Escaped String and another is called Raw String.

    • Escaped string is declared within double quote (” “) and may contain escape characters like ‘\n’, ‘\t’, ‘\b’ etc.
    • Raw string is declared within triple quote (“”” “””) and may contain multiple lines of text without any escape characters.
    funmain(args: Array<String>){val escapedString : String  ="I am escaped String!\n"var rawString :String  ="""This is going to be a
       multi-line string and will
       not have any escape sequence""";print(escapedString)println(rawString)}

    When you run the above Kotlin program, it will generate the following output:

    I am escaped String!
    This is going to be a
       multi-line string and will
       not have any escape sequence
    

    (d) Kotlin Boolean Data Type

    Boolean is very simple like other programming languages. We have only two values for Boolean data type – either true or false.

    funmain(args: Array<String>){val A: Boolean =true// defining a variable with true valueval B: Boolean =false// defining a variable with false valueprintln("Value of variable A "+ A )println("Value of variable B "+ B )}

    When you run the above Kotlin program, it will generate the following output:

    Value of variable A true
    Value of variable B false
    

    Boolean has a nullable counterpart Boolean? that can store a null value as below:

    val boolNull: Boolean?=null

    (e) Kotlin Array Data Type

    Kotlin arrays are a collection of homogeneous data. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

    We will study array in a separate chapter, for now let’s look at one example to define an array of integers and then access its one of the elements.

    funmain(args: Array<String>){val numbers: IntArray =intArrayOf(1,2,3,4,5)println("Value at 3rd position : "+ numbers[2])}

    When you run the above Kotlin program, it will generate the following output:

    Value at 3rd position : 3
    

    Kotlin Data Type Conversion

    Type conversion is a process in which the value of one data type is converted into another type. Kotlin does not support direct conversion of one numeric data type to another, For example, it is not possible to convert an Int type to a Long type:

    funmain(args: Array<String>){val x: Int =100val y: Long = x  // Not valid assignmentprintln(y)}

    When you run the above Kotlin program, it will generate the following output:

    main.kt:3:18: error: type mismatch: inferred type is Int but Long was expected
       val y: Long = x  // Not valid assignment
    

    To convert a numeric data type to another type, Kotlin provides a set of functions:

    • toByte()
    • toShort()
    • toInt()
    • toLong()
    • toFloat()
    • toDouble()
    • toChar()

    Now let’s rewrite above example once again and try to run it:

    funmain(args: Array<String>){val x: Int =100val y: Long = x.toLong()println(y)}

    When you run the above Kotlin program, it will generate the following output:

    100
    
  • Programming Style

    Programming style is all about following some rules while developing programs. These good practices impart values like readability, and unambiguity into your program.

    A good program should have the following characteristics −

    • Readability
    • Proper logical structure
    • Self-explanatory notes and comments

    For example, if you make a comment like the following, it will not be of much help −

    ! loop from 1 to 10 
    do i = 1,10  

    However, if you are calculating binomial coefficient, and need this loop for nCr then a comment like this will be helpful −

    ! loop to calculate nCr 
    do i = 1,10
    • Indented code blocks to make various levels of code clear.
    • Self-checking codes to ensure there will be no numerical errors like division by zero, square root of a negative real number or logarithm of a negative real number.
    • Including codes that ensure variables do not take illegal or out of range values, i.e., input validation.
    • Not putting checks where it would be unnecessary and slows down the execution. For example −
    real :: x 
    x = sin(y) + 1.0
    
    if (x >= 0.0) then
       z = sqrt(x)
    end if
    • Clearly written code using appropriate algorithms.
    • Splitting the long expressions using the continuation marker ‘&’.
    • Making meaningful variable names.
  • Variables

    Variables are an important part of any programming. They are the names you give to computer memory locations which are used to store values in a computer program and later you use those names to retrieve the stored values and use them in your program.

    Kotlin variables are created using either var or val keywords and then an equal sign = is used to assign a value to those created variables.

    Syntax

    Following is a simple syntax to create two variables and then assign them different values:

    var name ="Zara Ali"var age =19var height =5.2

    Examples

    Once a variable is created and assigned a value, later we can access its value using its name as follows:

    funmain(){var name ="Zara Ali"var age =19println(name)println(age)}

    When you run the above Kotlin program, it will generate the following output:

    Zara Ali
    19
    

    Let’s see one more example where we will access variable values using dollar sign $:

    funmain(){var name ="Zara Ali"var age =19println("Name = $name")println("Age = $age")}

    When you run the above Kotlin program, it will generate the following output:

    Name = Zara Ali
    Age = 19
    

    Let’s see one more example to display the variable values without using a dollar sign as below:

    funmain(){var name ="Zara Ali"var age =19println("Name = "+ name)println("Age = "+ age)}

    When you run the above Kotlin program, it will generate the following output:

    Name = Zara Ali
    Age = 19
    

    Kotlin Mutable Variables

    Mutable means that the variable can be reassigned to a different value after initial assignment. To declare a mutable variable, we use the var keyword as we have used in the above examples:

    funmain(){var name ="Zara Ali"var age =19println("Name = $name")println("Age = $age")
       
       name ="Nuha Ali"
       age =11println("Name = $name")println("Age = $age")}

    When you run the above Kotlin program, it will generate the following output:

    Name = Zara Ali
    Age = 19
    Name = Nuha Ali
    Age = 11
    

    Kotlin Read-only Variables

    A read-only variable can be declared using val (instead of var) and once a value is assigned, it can not be re-assigned.

    funmain(){val name ="Zara Ali"val age =19println("Name = $name")println("Age = $age")
       
       name ="Nuha Ali"// Not allowed, throws an exception
       age =11println("Name = $name")println("Age = $age")}

    When you run the above Kotlin program, it will generate the following output:

    main.kt:8:4: error: val cannot be reassigned
       name = "Nuha Ali" // Not allowed, throws an exception
       ^
    main.kt:9:4: error: val cannot be reassigned
       age = 11
       ^
    

    Read-only vs Mutable

    The Mutable variables will be used to define variables, which will keep charging their values based on different conditions during program execution.

    You will use Read-only variable to define different constant values i.e. the variables which will retain their value throughout of the program.

    Kotlin Variable Types

    Kotlin is smart enough to recognise that “Zara Ali” is a string, and that 19 is a number variable. However, you can explicitly specify a variable type while creating it:

    funmain(){var name: String ="Zara Ali"var age: Int =19println("Name = $name")println("Age = $age")
       
       name ="Nuha Ali"
       age =11println("Name = $name")println("Age = $age")}

    When you run the above Kotlin program, it will generate the following output:

    Name = Zara Ali
    Age = 19
    Name = Nuha Ali
    Age = 11
    

    Soon we will learn more about different data types available in Kotlin which can be used to create different type of variables.

    Kotlin Variable Naming Rules

    There are certain rules to be followed while naming the Kotlin variables:

    • Kotlin variable names can contain letters, digits, underscores, and dollar signs.
    • Kotlin variable names should start with a letter or underscores
    • Kotlin variables are case sensitive which means Zara and ZARA are two different variables.
    • Kotlin variable can not have any white space or other control characters.
    • Kotlin variable can not have names like var, val, String, Int because they are reserved keywords in Kotlin.
  • Program Libraries

    There are various Fortran tools and libraries. Some are free and some are paid services.

    Following are some free libraries −

    • RANDLIB, random number and statistical distribution generators
    • BLAS
    • EISPACK
    • GAMS–NIST Guide to Available Math Software
    • Some statistical and other routines from NIST
    • LAPACK
    • LINPACK
    • MINPACK
    • MUDPACK
    • NCAR Mathematical Library
    • The Netlib collection of mathematical software, papers, and databases.
    • ODEPACK
    • ODERPACK, a set of routines for ranking and ordering.
    • Expokit for computing matrix exponentials
    • SLATEC
    • SPECFUN
    • STARPAC
    • StatLib statistical library
    • TOMS
    • Sorting and merging strings

    The following libraries are not free −

    • The NAG Fortran numerical library
    • The Visual Numerics IMSL library
    • Numerical Recipes
  • Keywords

    Kotlin keywords are predefined, reserved words used in Kotlin programming that have special meanings to the compiler. These words cannot be used as an identifier (variables names, package names, function names etc.) and if used then compiler will raise an exception.

    Kotlin uses fun keyword to define a function, so if we we will try to use it as a variable name then it will be an exception. For example:

    funmain(){varfun="Zara Ali"// Not allowed, throws an exceptionvar age =19// Valid variable nameprintln("Name = $fun")println("Age = $age")}

    When you run the above Kotlin program, it will generate the following output:

    main.kt:2:7: error: expecting property name or receiver type
       var fun = "Zara Ali"  // Not allowed, throws an exception
    
      ^
    main.kt:2:11: error: expecting '(' var fun = "Zara Ali" // Not allowed, throws an exception
          ^
    main.kt:5:21: error: keyword cannot be used as a reference println("Name = $fun")
                    ^

    Kotlin keywords have been categorised into three broad categories: (a) Hard Keywords (b) Soft Keywords (c) Modifier Keywords

    As a good programming practice, it is highly recommended not to use any of the mentioned keywords to name any identifiers while coding in Kotlin.

    (a) Kotlin Hard Keywords

    Following is a list of hard keywords and they cannot be used as identifiers:

    asas?breakclass
    continuedoelsefalse
    forfunifin
    !ininterfaceis!is
    nullobjectpackagereturn
    superthisthrowtrue
    trytypealiastypeofval
    varwhenwhile

    (b) Kotlin Soft Keywords

    Following is the list of keywords (soft) in the context when they are applicable and can be used as identifiers in other contexts:

    bycatchconstructordelegate
    dynamicfieldfilefinally
    getimportinitparam
    propertyreceiversetsetparam
    valuewhere

    (c) Kotlin Modifier Keywords

    Following is the list of tokens which act as keywords in modifier lists of declarations and can be used as identifiers in other contexts:

    actualabstractannotationcompanion
    constcrossinlinedataenum
    expectexternalfinalinfix
    inlineinnerinternallateinit
    noinlineopenoperatorout
    overrideprivateprotectedpublic
    reifiedsealedsuspendtailrec
    vararg
  • Numeric Precision

    We have already discussed that, in older versions of Fortran, there were two real types: the default real type and double precision type.

    However, Fortran 90/95 provides more control over the precision of real and integer data types through the kind specifie.

    The Kind Attribute

    Different kind of numbers are stored differently inside the computer. The kind attribute allows you to specify how a number is stored internally. For example,

    real, kind = 2 :: a, b, c
    real, kind = 4 :: e, f, g
    integer, kind = 2 :: i, j, k
    integer, kind = 3 :: l, m, n
    

    In the above declaration, the real variables e, f and g have more precision than the real variables a, b and c. The integer variables l, m and n, can store larger values and have more digits for storage than the integer variables i, j and k. Although this is machine dependent.

    Example

    Live Demo

    program kindSpecifier
    implicit none
    
       real(kind = 4) :: a, b, c
       real(kind = 8) :: e, f, g
       integer(kind = 2) :: i, j, k
       integer(kind = 4) :: l, m, n
       integer :: kind_a, kind_i, kind_e, kind_l
       
       kind_a = kind(a)
       kind_i = kind(i)
       kind_e = kind(e)
       kind_l = kind(l)
       
       print *,'default kind for real is', kind_a
       print *,'default kind for int is', kind_i
       print *,'extended kind for real is', kind_e
       print *,'default kind for int is', kind_l
       
    end program kindSpecifier

    When you compile and execute the above program it produces the following result −

    default kind for real is 4
    default kind for int is 2
    extended kind for real is 8
    default kind for int is 4
    

    Inquiring the Size of Variables

    There are a number of intrinsic functions that allows you to interrogate the size of numbers.

    For example, the bit_size(i) intrinsic function specifies the number of bits used for storage. For real numbers, the precision(x) intrinsic function, returns the number of decimal digits of precision, while the range(x) intrinsic function returns the decimal range of the exponent.

    Example

    Live Demo

    program getSize
    implicit none
    
       real (kind = 4) :: a
       real (kind = 8) :: b
       integer (kind = 2) :: i
       integer (kind = 4) :: j
    
       print *,'precision of real(4) =', precision(a)
       print *,'precision of real(8) =', precision(b)
       
       print *,'range of real(4) =', range(a)
       print *,'range of real(8) =', range(b)
       
    
       print *,'maximum exponent of real(4) =' , maxexponent(a)
       print *,'maximum exponent of real(8) =' , maxexponent(b)
      
       print *,'minimum exponent of real(4) =' , minexponent(a)
       print *,'minimum exponent of real(8) =' , minexponent(b)
       
       print *,'bits in integer(2) =' , bit_size(i)
       print *,'bits in integer(4) =' , bit_size(j)
       
    end program getSize

    When you compile and execute the above program it produces the following result −

    precision of real(4) = 6
    precision of real(8) = 15
    range of real(4) = 37
    range of real(8) = 307
    maximum exponent of real(4) = 128
    maximum exponent of real(8) = 1024
    minimum exponent of real(4) = -125
    minimum exponent of real(8) = -1021
    bits in integer(2) = 16
    bits in integer(4) = 32
    

    Obtaining the Kind Value

    Fortran provides two more intrinsic functions to obtain the kind value for the required precision of integers and reals −

    • selected_int_kind (r)
    • selected_real_kind ([p, r])

    The selected_real_kind function returns an integer that is the kind type parameter value necessary for a given decimal precision p and decimal exponent range r. The decimal precision is the number of significant digits, and the decimal exponent range specifies the smallest and largest representable number. The range is thus from 10-r to 10+r.

    For example, selected_real_kind (p = 10, r = 99) returns the kind value needed for a precision of 10 decimal places, and a range of at least 10-99 to 10+99.

    Example

    Live Demo

    program getKind
    implicit none
    
       integer:: i
       i = selected_real_kind (p = 10, r = 99) 
       print *,'selected_real_kind (p = 10, r = 99)', i
       
    end program getKind

    When you compile and execute the above program it produces the following result −

    selected_real_kind (p = 10, r = 99) 8
    
  • Comments

    A comment is a programmer-readable explanation or annotation in the Kotlin source code. They are added with the purpose of making the source code easier for humans to understand, and are ignored by Kotlin compiler.

    Just like most modern languages, Kotlin supports single-line (or end-of-line) and multi-line (block) comments. Kotlin comments are very much similar to the comments available in Java, C and C++ programming languages.

    Kotlin Single-line Comments

    Single line comments in Kotlin starts with two forward slashes // and end with end of the line. So any text written in between // and the end of the line is ignored by Kotlin compiler.

    Following is the sample Kotlin program which makes use of a single-line comment:

    // This is a commentfunmain(){println("Hello, World!")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, World!
    

    A single line comment can start from anywhere in the program and will end till the end of the line. For example, you can use single line comment as follows:

    funmain(){println("Hello, World!")// This is also a comment}

    Kotlin Multi-line Comments

    A multi-line comment in Kotlin starts with /* and end with */. So any text written in between /* and */ will be treated as a comment and will be ignored by Kotlin compiler.

    Multi-line comments also called Block comments in Kotlin.

    Following is the sample Kotlin program which makes use of a multi-line comment:

    /* This is a multi-line comment and it can span
     * as many lines as you like 
     */funmain(){println("Hello, World!")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, Word!
    

    Kotlin Nested Comments

    Block comments in Kotlin can be nested, which means a single-line comment or multi-line comments can sit inside a multi-line comment as below:

    /* This is a multi-line comment and it can span
     * as many lines as you like 
     /* This is a nested comment */// Another nested comment */funmain(){println("Hello, World!")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, World!
    
  • Intrinsic Functions

    Intrinsic functions are some common and important functions that are provided as a part of the Fortran language. We have already discussed some of these functions in the Arrays, Characters and String chapters.

    Intrinsic functions can be categorised as −

    • Numeric Functions
    • Mathematical Functions
    • Numeric Inquiry Functions
    • Floating-Point Manipulation Functions
    • Bit Manipulation Functions
    • Character Functions
    • Kind Functions
    • Logical Functions
    • Array Functions.

    We have discussed the array functions in the Arrays chapter. In the following section we provide brief descriptions of all these functions from other categories.

    In the function name column,

    • A represents any type of numeric variable
    • R represents a real or integer variable
    • X and Y represent real variables
    • Z represents complex variable
    • W represents real or complex variable

    Numeric Functions

    Sr.NoFunction & Description
    1ABS (A)It returns the absolute value of A
    2AIMAG (Z)It returns the imaginary part of a complex number Z
    3AINT (A [, KIND])It truncates fractional part of A towards zero, returning a real, whole number.
    4ANINT (A [, KIND])It returns a real value, the nearest integer or whole number.
    5CEILING (A [, KIND])It returns the least integer greater than or equal to number A.
    6CMPLX (X [, Y, KIND])It converts the real variables X and Y to a complex number X+iY; if Y is absent, 0 is used.
    7CONJG (Z)It returns the complex conjugate of any complex number Z.
    8DBLE (A)It converts A to a double precision real number.
    9DIM (X, Y)It returns the positive difference of X and Y.
    10DPROD (X, Y)It returns the double precision real product of X and Y.
    11FLOOR (A [, KIND])It provides the greatest integer less than or equal to number A.
    12INT (A [, KIND])It converts a number (real or integer) to integer, truncating the real part towards zero.
    13MAX (A1, A2 [, A3,…])It returns the maximum value from the arguments, all being of same type.
    14MIN (A1, A2 [, A3,…])It returns the minimum value from the arguments, all being of same type.
    15MOD (A, P)It returns the remainder of A on division by P, both arguments being of the same type (A-INT(A/P)*P)
    16MODULO (A, P)It returns A modulo P: (A-FLOOR(A/P)*P)
    17NINT (A [, KIND])It returns the nearest integer of number A
    18REAL (A [, KIND])It Converts to real type
    19SIGN (A, B)It returns the absolute value of A multiplied by the sign of P. Basically it transfers the of sign of B to A.

    Example

    program numericFunctions
    implicit none  
    
       ! define constants  
       ! define variables
       real :: a, b 
       complex :: z
       
       ! values for a, b 
       a = 15.2345
       b = -20.7689
    
    write(*,*) 'abs(a): ',abs(a),' abs(b): ',abs(b) write(*,*) 'aint(a): ',aint(a),' aint(b): ',aint(b) write(*,*) 'ceiling(a): ',ceiling(a),' ceiling(b): ',ceiling(b) write(*,*) 'floor(a): ',floor(a),' floor(b): ',floor(b)
    z = cmplx(a, b) write(*,*) 'z: ',z end program numericFunctions

    When you compile and execute the above program, it produces the following result −

    abs(a): 15.2344999   abs(b): 20.7688999    
    aint(a): 15.0000000  aint(b): -20.0000000    
    ceiling(a): 16  ceiling(b): -20
    floor(a): 15  floor(b): -21
    z: (15.2344999, -20.7688999)
    

    Mathematical Functions

    Sr.NoFunction & Description
    1ACOS (X)It returns the inverse cosine in the range (0, π), in radians.
    2ASIN (X)It returns the inverse sine in the range (-π/2, π/2), in radians.
    3ATAN (X)It returns the inverse tangent in the range (-π/2, π/2), in radians.
    4ATAN2 (Y, X)It returns the inverse tangent in the range (-π, π), in radians.
    5COS (X)It returns the cosine of argument in radians.
    6COSH (X)It returns the hyperbolic cosine of argument in radians.
    7EXP (X)It returns the exponential value of X.
    8LOG (X)It returns the natural logarithmic value of X.
    9LOG10 (X)It returns the common logarithmic (base 10) value of X.
    10SIN (X)It returns the sine of argument in radians.
    11SINH (X)It returns the hyperbolic sine of argument in radians.
    12SQRT (X)It returns square root of X.
    13TAN (X)It returns the tangent of argument in radians.
    14TANH (X)It returns the hyperbolic tangent of argument in radians.

    Example

    The following program computes the horizontal and vertical position x and y respectively of a projectile after a time, t −

    Where, x = u t cos a and y = u t sin a - g t2 / 2
    program projectileMotion  
    implicit none  
    
       ! define constants  
       real, parameter :: g = 9.8  
       real, parameter :: pi = 3.1415927  
       
       !define variables
       real :: a, t, u, x, y   
       
       !values for a, t, and u 
       a = 45.0
       t = 20.0
       u = 10.0
       
       ! convert angle to radians  
       a = a * pi / 180.0  
       x = u * cos(a) * t   
       y = u * sin(a) * t - 0.5 * g * t * t  
       
       write(*,*) 'x: ',x,'  y: ',y   
       
    end program projectileMotion

    When you compile and execute the above program, it produces the following result −

    x: 141.421356  y: -1818.57861  
    

    Numeric Inquiry Functions

    These functions work with a certain model of integer and floating-point arithmetic. The functions return properties of numbers of the same kind as the variable X, which can be real and in some cases integer.

    Sr.NoFunction & Description
    1DIGITS (X)It returns the number of significant digits of the model.
    2EPSILON (X)It returns the number that is almost negligible compared to one. In other words, it returns the smallest value such that REAL( 1.0, KIND(X)) + EPSILON(X) is not equal to REAL( 1.0, KIND(X)).
    3HUGE (X)It returns the largest number of the model
    4MAXEXPONENT (X)It returns the maximum exponent of the model
    5MINEXPONENT (X)It returns the minimum exponent of the model
    6PRECISION (X)It returns the decimal precision
    7RADIX (X)It returns the base of the model
    8RANGE (X)It returns the decimal exponent range
    9TINY (X)It returns the smallest positive number of the model

    Floating-Point Manipulation Functions

    Sr.NoFunction & Description
    1EXPONENT (X)It returns the exponent part of a model number
    2FRACTION (X)It returns the fractional part of a number
    3NEAREST (X, S)It returns the nearest different processor number in given direction
    4RRSPACING (X)It returns the reciprocal of the relative spacing of model numbers near given number
    5SCALE (X, I)It multiplies a real by its base to an integer power
    6SET_EXPONENT (X, I)it returns the exponent part of a number
    7SPACING (X)It returns the absolute spacing of model numbers near given number

    Bit Manipulation Functions

    Sr.NoFunction & Description
    1BIT_SIZE (I)It returns the number of bits of the model
    2BTEST (I, POS)Bit testing
    3IAND (I, J)Logical AND
    4IBCLR (I, POS)Clear bit
    5IBITS (I, POS, LEN)Bit extraction
    6IBSET (I, POS)Set bit
    7IEOR (I, J)Exclusive OR
    8IOR (I, J)Inclusive OR
    9ISHFT (I, SHIFT)Logical shift
    10ISHFTC (I, SHIFT [, SIZE])Circular shift
    11NOT (I)Logical complement

    Character Functions

    Sr.NoFunction & Description
    1ACHAR (I)It returns the Ith character in the ASCII collating sequence.
    2ADJUSTL (STRING)It adjusts string left by removing any leading blanks and inserting trailing blanks
    3ADJUSTR (STRING)It adjusts string right by removing trailing blanks and inserting leading blanks.
    4CHAR (I [, KIND])It returns the Ith character in the machine specific collating sequence
    5IACHAR (C)It returns the position of the character in the ASCII collating sequence.
    6ICHAR (C)It returns the position of the character in the machine (processor) specific collating sequence.
    7INDEX (STRING, SUBSTRING [, BACK])It returns the leftmost (rightmost if BACK is .TRUE.) starting position of SUBSTRING within STRING.
    8LEN (STRING)It returns the length of a string.
    9LEN_TRIM (STRING)It returns the length of a string without trailing blank characters.
    10LGE (STRING_A, STRING_B)Lexically greater than or equal
    11LGT (STRING_A, STRING_B)Lexically greater than
    12LLE (STRING_A, STRING_B)Lexically less than or equal
    13LLT (STRING_A, STRING_B)Lexically less than
    14REPEAT (STRING, NCOPIES)Repeated concatenation
    15SCAN (STRING, SET [, BACK])It returns the index of the leftmost (rightmost if BACK is .TRUE.) character of STRING that belong to SET, or 0 if none belong.
    16TRIM (STRING)Removes trailing blank characters
    17VERIFY (STRING, SET [, BACK])Verifies the set of characters in a string

    AD

    https://delivery.adrecover.com/recover.html?siteId=18107&dataDogLoggingEnabled=false&dataDogLoggingVersion=1

    Kind Functions

    Sr.NoFunction & Description
    1KIND (X)It returns the kind type parameter value.
    2SELECTED_INT_KIND (R)It returns kind of type parameter for specified exponent range.
    3SELECTED_REAL_KIND ([P, R])Real kind type parameter value, given precision and range

    Logical Function

    Sr.NoFunction & Description
    1LOGICAL (L [, KIND])Convert between objects of type logical with different kind type parameters
  • Basic Syntax

    Kotlin Program Entry Point

    An entry point of a Kotlin application is the main() function. A function can be defined as a block of code designed to perform a particular task.

    Let’s start with a basic Kotlin program to print “Hello, World!” on the standard output:

    funmain(){var string: String  ="Hello, World!"println("$string")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, World!
    

    Entry Point with Parameters

    Another form of main() function accepts a variable number of String arguments as follows:

    funmain(args: Array<String>){println("Hello, world!")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, World!
    

    If you have observed, its clear that both the programs generate same output, so it is very much optional to pass a parameter in main() function starting from Kotlin version 1.3.

    print() vs println()

    The print() is a function in Kotlin which prints its argument to the standard output, similar way the println() is another function which prints its argument on the standard output but it also adds a line break in the output.

    Let’s try the following program to understand the difference between these two important functions:

    funmain(args: Array<String>){println("Hello,")println(" world!")print("Hello,")print(" world!")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, 
     world!
    Hello, world!
    

    Both the functions (print() and println()) can be used to print numbers as well as strings and at the same time to perform any mathematical calculations as below:

    funmain(args: Array<String>){println(200)println("200")println(2+2)print(4*3)}

    When you run the above Kotlin program, it will generate the following output:

    200
    200
    4
    12
    

    Semicolon (;) in Kotlin

    Kotlin code statements do not require a semicolon (;) to end the statement like many other programming languages, such as Java, C++, C#, etc. do need it.

    Though you can compile and run a Kotlin program with and without semicolon successfully as follows:

    funmain(){println("I'm without semi-colon")println("I'm with semi-colon");}

    When you run the above Kotlin program, it will generate the following output:

    I'm without semi-colon
    I'm with semi-colon
    

    So as a good programming practice, it is not recommended to add a semicolon in the end of a Kotlin statement.

    Packages in Kotlin

    Kotlin code is usually defined in packages though package specification is optional. If you don’t specify a package in a source file, its content goes to the default package.

    If we specify a package in Kotlin program then it is specified at the top of the file as follows:

    package org.tutorialspoint.com
    
    funmain(){println("Hello, World!")}

    When you run the above Kotlin program, it will generate the following output:

    Hello, World!
    
  • Architecture

    Kotlin is a programming language and has its own architecture to allocate memory and produce a quality output to the end user.

    Following are the different scenarios where Kotlin compiler will work differently.

    • Compile Kotlin into bytecode which can run on JVM. This bytecode is exactly equal to the byte code generated by the Java .class file.
    • Whenever Kotlin targets JavaScript, the Kotlin compiler converts the .kt file into ES5.1 and generates a compatible code for JavaScript.
    • Kotlin compiler is capable of creating platform basis compatible codes via LLVM.
    • Kotlin Multiplatform Mobile (KMM) is used to create multiplatform mobile applications with code shared between Android and iOS.
    kotlin Architecture

    Whenever two byte coded files ( Two different programs from Kotlin and Java) runs on the JVM, they can communicate with each other and this is how an interoperable feature is established in Kotlin for Java.

    Kotlin Native

    Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. Kotlin/Native supports the following platforms:

    • macOS
    • iOS, tvOS, watchOS
    • Linux
    • Windows (MinGW)
    • Android NDK
    • Many more…

    Kotlin/Native is primarily designed to allow compilation for platforms where virtual machines are not desirable or possible, for example, embedded devices or iOS.

    It is easy to include a compiled Kotlin code into existing projects written in C, C++, Swift, Objective-C, and other languages.