HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to FortranVariablesOperatorsConditions (if)Loops (do)ArraysFunctions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
🔁 Condition & Loop
📋 Array
🧩 Functions & Subroutines
functionsubroutine
Fortran›REFERENCE›Functions & Subroutines
🧩

Functions & Subroutines

function and subroutine.

CommandDescription
functionSubroutine that returns a value.
subroutineSubroutine that does not return a value.

function

Try it yourself →

Subroutine that returns a value.

Syntax

function name(x)
  name = ...
end function

Description

The function name carries the return value (name = result). Defined after the contains block within the program.

Example

program h
  print *, square(5)
contains
  integer function square(x)
    integer :: x
    square = x * x
  end function square
end program h

Expected output

25

When to use?

Preventing code duplication, calculation units.

Common mistakes

  • ⚠️ The return value is assigned to the function's NAME: square = x*x.

subroutine

Try it yourself →

Subroutine that does not return a value.

Syntax

call name(arg)

Description

Does not return a value; called with call. Returns results by modifying arguments.

Example

program h
  call greet()
contains
  subroutine greet()
    print *, "Hello"
  end subroutine greet
end program h

Expected output

Hello

When to use?

Operations with side effects, procedures.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code