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
Fortran›Functions

Functions

Define a function in the contains block; the return value is assigned to the function name.

Simple enough for a 5-year-old to understand

function definition

Functions are defined after the "contains" block within the program. The return value is assigned to the NAME of the function.

Example code
1
program h
2
print *, square(5)
Call: 25
Output / InputCall: 25
3
contains
Subprograms from this point on.
DefinitionSubprograms from this point on.
4
integer function square(x)
Return type + function.
FunctionReturn type + function.
5
integer :: x
Parameter type.
VariableParameter type.
6
square = x * x
Assign result to the NAME.
FunctionAssign result to the NAME.
7
end function square
8
end program h
Output / InputDefinitionFunctionVariable
💡 Hint: The return value is assigned to the function name: square = x * x (there is no return).

About this topic

In Fortran, subprograms that return a value are called functions, and those that do not are called subroutines. A function is used inside an expression; a subroutine is executed with a separate call statement.

In Fortran, parameters are passed by reference: modifying a parameter inside a subprogram also changes the caller's variable. This is the exact opposite of the Pascal family, and to prevent unintended modifications, you must declare the intent of each parameter.

Intent declaration — input, output, or both — provides both documentation and safety. If you try to assign a value to a parameter marked as input, the compiler throws an error, which prevents hard-to-find bugs.

What you will learn

  • Distinguishing between functions and subroutines
  • Returning a value
  • Declaring parameter intent
  • Using modules
  • The risk of implicit interfaces

Who is this for?

  • Those who divide their code into subprograms
  • Those dealing with unexpected modifications
  • Those modularizing legacy code

When to use?

Subprograms must always be defined inside a module; this way, the compiler sees the interface and catches call errors at compile time.

The intent of every parameter must be declared; this tells anyone reading the code which values will change and gives the compiler optimization opportunities.

Common mistakes

Wrong

program test
  implicit none
  integer :: sonuc
  sonuc = topla(2, 3)
  print *, sonuc
end program test

function topla(a, b)
  implicit none
  integer :: a, b, topla
  topla = a + b
end function topla

Since the function is defined outside the program, the compiler does not see its interface; an implicit interface is created, and the return type is guessed using implicit typing rules. Even if the number or type of arguments is wrong, no compilation error occurs. Subprograms must be placed inside modules.

Correct

module islemler
  implicit none
contains
  function topla(a, b) result(s)
    integer, intent(in) :: a, b
    integer :: s
    s = a + b
  end function topla
end module islemler

program test
  use islemler
  implicit none
  print *, topla(2, 3)
end program test

Wrong

module islemler
  implicit none
contains
  subroutine yazdir(x)
    integer :: x
    x = x * 2
    print *, x
  end subroutine yazdir
end module islemler

program test
  use islemler
  implicit none
  integer :: deger
  deger = 5
  call yazdir(deger)
  print *, deger
end program test

Since the parameter is passed by reference and its intent is not declared, the subroutine permanently changes the caller's variable; ten is printed at the end. If you only want to read it, you need to mark the parameter as input; then an assignment attempt becomes a compilation error.

Correct

module islemler
  implicit none
contains
  subroutine yazdir(x)
    integer, intent(in) :: x
    print *, x * 2
  end subroutine yazdir
end module islemler

program test
  use islemler
  implicit none
  integer :: deger
  deger = 5
  call yazdir(deger)
  print *, deger
end program test

Wrong

module islemler
  implicit none
contains
  function topla(a, b) result(s)
    integer, intent(in) :: a, b
    integer :: s
    print *, a + b
  end function topla
end module islemler

program test
  use islemler
  implicit none
  print *, topla(2, 3)
end program test

No assignment was made to the function's result variable; printing to the screen is not returning. The return value remains undefined and a meaningless number appears in the output. The compiler does not always report this situation as a warning.

Correct

module islemler
  implicit none
contains
  function topla(a, b) result(s)
    integer, intent(in) :: a, b
    integer :: s
    s = a + b
  end function topla
end module islemler

program test
  use islemler
  implicit none
  print *, topla(2, 3)
end program test

Frequently asked questions

Why is intent declaration important?

Since parameters are passed by reference, without a declaration every parameter is mutable. Marking it as input catches accidental modifications at compile time and allows the compiler to optimize by assuming the value remains constant.

What is the difference between a module and an include section?

A module is a separate compilation unit and can be used by many programs. Subprograms defined in a program's own include section belong only to that program, but their interfaces are still visible. Both solve the implicit interface problem.

What is a pure function?

It is a function that has no side effects and produces a result solely from its arguments, and it is marked with a separate attribute. When the compiler knows this, it can perform optimizations such as parallelization and call elimination.

Summary

Functions return a value, subroutines do not.

Parameters are passed by reference.

The intent of every parameter must be declared.

Subprograms should be defined within a module.

Implicit interfaces hide call errors.

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