Menu Close

EXTERNAL Statement – FORTRAN

In Fortran, the EXTERNAL statement is used to declare a procedure (subroutine or function) as an external entity.

The EXTERNAL statement is typically used when you want to use a procedure (function or subroutine) that is defined in a separate source file.

Example

Consider two files:

one containing the external subroutine my_subroutine and another file containing the main program external_example.

The EXTERNAL statement is used in the main program to declare my_subroutine as an external procedure.

 

The my_subroutine is defined in a separate file and can be compiled separately. The main program then calls the external subroutine using the call statement.



! Subroutine defined in a separate file
subroutine my_subroutine(arg)
  implicit none
  integer, intent(in) :: arg
  ! Subroutine body
  ...
end subroutine my_subroutine

! Main program
program external_example
  implicit none
  external my_subroutine  ! Declare the external subroutine

  integer :: value

  ! Read input value
  write(*,*) "Enter a value:"
  read(*,*) value

  ! Call the external subroutine
  call my_subroutine(value)

  ! Continue with the main program
  ...
end program external_example

 

More Related Stuff