{"id":22225513,"url":"https://github.com/beliavsky/julia-vs.-fortran-syntax","last_synced_at":"2026-02-25T21:01:56.657Z","repository":{"id":129408059,"uuid":"371693666","full_name":"Beliavsky/Julia-vs.-Fortran-syntax","owner":"Beliavsky","description":"Comparison of Julia and Fortran syntax","archived":false,"fork":false,"pushed_at":"2021-06-06T00:44:29.000Z","size":87,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-01-29T08:46:52.821Z","etag":null,"topics":["fortran","julia"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Beliavsky.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-05-28T12:29:05.000Z","updated_at":"2023-12-22T05:28:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"adc48ceb-668e-43a7-ba70-b963bac3d8d7","html_url":"https://github.com/Beliavsky/Julia-vs.-Fortran-syntax","commit_stats":{"total_commits":43,"total_committers":1,"mean_commits":43.0,"dds":0.0,"last_synced_commit":"b3284abb796fc0ea267e0abfa9c803df251876d0"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beliavsky%2FJulia-vs.-Fortran-syntax","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beliavsky%2FJulia-vs.-Fortran-syntax/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beliavsky%2FJulia-vs.-Fortran-syntax/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Beliavsky%2FJulia-vs.-Fortran-syntax/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Beliavsky","download_url":"https://codeload.github.com/Beliavsky/Julia-vs.-Fortran-syntax/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227824204,"owners_count":17825147,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fortran","julia"],"created_at":"2024-12-03T00:18:14.843Z","updated_at":"2025-10-07T11:26:59.508Z","avatar_url":"https://github.com/Beliavsky.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Julia vs. Fortran syntax\n\nHere are some notes on equivalent syntax in Julia and Fortran. Below, the Julia syntax appears first and the Fortran syntax appears after \"vs.\". Additions and corrections are welcomed -- please raise an issue.\n\n`x = 1.0` vs. `x = 1.0d0` (Julia uses 64-bit floats by default)\n\n`x = 1.0f0` vs. `x = 1.0` (single precision) \n\n`true` and `false` vs. `.true.` and `.false.`\n\n`const n = 3` vs. `integer, parameter :: n = 3`\n\n`const name = \"Ed\"` vs. `character (len=*), parameter :: name = \"Ed\"`\n\n`const tf = true` vs. `logical, parameter :: tf = .true.`\n\nFortran does not have the update operations that Julia does, so\n\n`x += 2` vs. `x = x + 2`\n\n`x -= 2` vs. `x = x - 2`\n\n`x *= 2` vs. `x = x * 2`\n\n`x /= 2` vs. `x = x / 2`\n\n`x = zeros(5)` # array of Float64 intialized to zero \n\nvs. Fortran\n\n     real(kind=kind(1.0d0)), allocatable :: x(:)\n     allocate (x(5),source=0.0d0)\n\n`[]` vs. `()` to index array elements\n\n`x[1:2:5]` vs. `x(1:5:2)` to get elements `[x(1),x(3),x(5)]` since Julia uses start:[step:]stop and Fortran uses start:stop[:step]\n\n`\u0026` vs `.and.`\n\n`|` vs. `.or.`\n\n`!` vs. `.not.`\n\n`^` vs. `**` for exponentiation.\n\n`x = rand()` vs. `call random_number(x)` where `x` is a scalar\n\n`x = rand(n)` vs. `call random_number(x)` where `x` is a vector of length `n` \n\n`x = rand(n1,n2)` vs. `call random_number(x)` where `x` has shape `[n1,n2]`\n\n`sum(x)` has the same meaning in Julia and Fortran\n\n`sum(x,dims=1)` vs. `sum(x,dim=1)`\n\n`sum(x,dims=[1,2])` = `sum(x)` for 2-D array\n\nIf `x` is an array, `sin.(x)` vs. `sin(x)` and the same for other functions that accept scalar arguments. \nJulia functions need a `.` before the argument(s) to broadcast. A user-defined Fortran function or subroutine\nthat accepts either scalar or array arguments is declared ELEMENTAL.\n\nIn general, Julia has a `dims` optional argument for array functions vs. `dim` in Fortran,\n\n`minimum(x)` and `maximum(x)` vs. `minval(x)` and `maxval(x)`\n\n`max(2,3.1)` vs. `max(real(2),3.1)` or `max(2.0,3.1)` -- the Fortran `max` requires arguments of the same type.\n\nJulia `max.(x,y)` for arrays `x` and `y` is similar to Fortran `max(x,y)`, but Julia `max(x,y)` (without the `.`) for arrays x and y compares the first elements of the two arrays and returns the array with largest 1st element, looking at the 2nd elements etc. to break ties. Fortran has no simple equivalent.\n\n`size(x)` returns tuple, `size(x,dim)` returns scalar. Fortran `size(x)` returns a scalar. \n`size(x)` vs. `shape(x)`\n`length(x)` vs. `size(x)`\n\n`x = [2,3]` creates a 1-D array of integers \n`x = [2,3.1]` creates a 1-D array of Float64, `[2.0,3.1]`. The integer value is coerced to Float64. In Fortran the elements of an array constructor must have the same type.\n\n`vec(x)` converts x to 1-D array, vs. `[x]`\n\n`x .\u003e 0` vs. `merge(1,0,x\u003e0)`    Note the . before \u003e.\n`x[x .\u003e 0]` vs. `pack(x,x\u003e0)`\n\n`*` vs. `//` to concatenate strings\n\n`rstrip(\"foo \")` vs. `trim(\"foo \")`\n\n`println(2,\" \",5)` vs. `print*,2,5` -- Julia does not insert spaces between printed items by default.\n\n`print(2)` vs. `write(*,\"(i0)\",advance=\"no\") 2` -- Julia `print` does not append newline, unlike `println`\n \n`#` vs. `!` for comments\n\n`dog` and `Dog` are distinct variables in Julia but not in Fortran\n\n`argmax(x)` vs. `maxloc(x,dim=1)` for 1-D array `x`\n\n`sum([true,false,true])` vs. `count([.true.,.false.,.true.])`\n\nFor logical variables `x` and `y`, `x == y` vs. `x .eqv. y`\n\nloops in Julia\n\n    for i in 1:3\n        println(i,\" \",i^2)\n    end\n\nvs. Fortran\n\n    do i=1,3\n        print*,i,i**2\n    end do\n    \nIf block in Julia\n\n    if someVar \u003e 10\n        println(\"someVar is totally bigger than 10.\")\n    elseif someVar \u003c 10    # This elseif clause is optional.\n        println(\"someVar is smaller than 10.\")\n    else                    # The else clause is optional too.\n        println(\"someVar is indeed 10.\")\n    end\n    \nvs. Fortran\n\n    if (someVar \u003e 10) then\n        print*,\"someVar is totally bigger than 10.\"\n    else if (someVar \u003c 10) then ! This elseif clause is optional.\n        print*,\"someVar is smaller than 10.\"\n    else                    ! The else clause is optional too.\n        print*,\"someVar is indeed 10.\"\n    end if\n    \nOne-line if in Julia\n\n     (i \u003e 1) \u0026\u0026 println(\"i \u003e 1\") \n\nvs. Fortran\n\n     if (i \u003e 1) print*,\"i \u003e 1\"\n\nExiting a loop early in Julia\n\n     for i in 1:5\n          println(i)\n          if i^2 \u003e 4\n               break\n          end\n     end\n\nvs. Fortran\n\n     do i=1,5\n          print*,i\n          if (i**2 \u003e 4) exit\n     end do\n    \nFunction definition in Julia\n\n    function power(i,a)\n        return i^a\n    end\n\nvs. Fortran\n\n    integer function power(i,a)\n        power = i**a\n    end\n\nArrays of strings in Julia\n\n     x = [\"boy\",\"girl\",\"man\"]\n     \nvs. Fortran\n\n     character (len=4), allocatable :: x(:)\n     x = [character (len=4) :: \"boy\",\"girl\",\"man\"]\n     x = [\"boy \",\"girl\",\"man \"] ! alternative with padding, since the character variables in a Fortran array \n                                ! must have the same length.\n        \nModule imports:        \n`using Foo` vs. `use Foo`\n`import Foo: bar` vs. `use Foo, only: bar`\n\nIn Fortran but not Julia, module imports must occur before the executable statements.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeliavsky%2Fjulia-vs.-fortran-syntax","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbeliavsky%2Fjulia-vs.-fortran-syntax","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbeliavsky%2Fjulia-vs.-fortran-syntax/lists"}