0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

fortranの基本

Last updated at Posted at 2025-04-13

環境

  • OS:windows11
  • ターミナル:bash

参考記事

詳細は上記記事を参照してほしいのですが、自分で使ってみて最低限必要だなと思える知識のみを以下で示します。

プログラムの基本

暗黙型宣言をキャンセル

f90
implicit none

fortranは何も宣言しなくても変数名の文頭の数字やアルファベットに則して型がついてしまいます。なので、上記のように宣言することでそのようなエラーのもととなる動作をキャンセルしておきます。(慣れている人は別ですが)

プログラム全体の例

f90
program calc
  implicit none
  integer a, b, c, d
  real e, f

  a = 1+3           ! a に 1+3 の結果(4)を代入
  b = a-2           ! b に a-2 を代入
  c = a*2           ! c に a*2 を代入
  d = 1/a           ! d に 1/a を代入(小数点以下切り捨て)
  print *, a, b, c, d
  e = 6.0/2.0       ! e に 6÷2 を代入
  f = e**3.0        ! e の 3 乗を f へ代入

  print *, e, f, g

  ! === 結果出力 ===
  call calc_g()

contains
  subroutine calc_g()
    double precision g
    g = 4d0*(2d0+1d0) ! かっこを使って足し算を先に行う例
    print *, g
  end subroutine calc_g

end program calc

メインプログラムはprogramからend programである。また、サブルーチン(関数)を書くこともできる。また、matlabと同じでfor文は0ではなく1から始まります。

プログラムの実行

dockerなどでgfortranの設定をしたのち、以下のコマンドを打つと、コンパイルできる。

bash
gfortran hello.f90
./a.out

最後に

スーパーコンピュータで今でも使われているfortranですが、昔の言語ということがあってあまり記事が見つからなかったので、自分で記事を書いてみることにしました。どなたかの参考になれば幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?