組み込み関数 - built-in function(Fortran 第2回)

←第1回 標準入出力
→第3回 多重ループ

はじめに

第2回は組み込み関数です。
Fortranは科学分野で活躍する言語で、言語の一部として計算などを行う組み込み関数が提供されています。

組み込み関数とは

Fortranが言語の一部として提供する関数で、数学計算、論理演算、記号列処理など広い分野の演算を行う関数が用意されています。
あまりにも種類が多すぎるので、自分がよく使う関数以外は、その時その時でググればよいでしょう。

具体例

program builtin_functions
    implicit none
    real :: x = 3.14
    integer :: i = 10
    character(len=10) :: str = 'Hello'

    ! Use ABS() to get the absolute value of a number
    print *, 'Absolute value of -x: ', abs(-x)

    ! Use LOG() to compute the natural logarithm of a number
    print *, 'Natural logarithm of x: ', log(x)

    ! Use LEN() to get the length of a string
    print *, 'Length of str: ', len(str)

    ! Use TRIM() to remove leading and trailing spaces from a string
    print *, 'Trimmed str: ', trim(str)

    ! Use MAXVAL() to find the maximum value in an array
    real :: arr(3) = [1.0, 2.0, 3.0]
    print *, 'Maximum value in arr: ', maxval(arr)
end program builtin_functions

疑問点

「組み込み関数は合計何個あるの?」

追加され続けているのではっきりとした個数はわかりませんが、主要なものでも約150個です。

まとめ

今回は組み込み関数について解説しました。正直解説するまでもない内容でしたね。
第3回は多重ループです。

注意

このページは大学のFortranの授業の予習で書いています。
直観的な理解をメモしているだけなので、厳密なところでは誤りがあるかもしれません。

←第1回 標準入出力
→第3回 多重ループ