External Functions vs Internal Functions in Fortran

In programming, functions are used to perform reusable computations or operations and return a value. Fortran supports both internal and external functions, allowing programmers to organize code efficiently, improve readability, and reuse functionality across multiple programs.

Understanding the distinction between internal and external functions is essential for writing modular, maintainable, and scalable Fortran programs.

1. Introduction to Functions in Fortran

A function is a subprogram that:

  • Takes input parameters
  • Performs a calculation or operation
  • Returns a single value

Functions help:

  • Avoid code duplication
  • Encapsulate logic for clarity
  • Facilitate testing and debugging

Fortran allows functions to be defined either inside the main program or outside as standalone subprograms. These are referred to as internal functions and external functions, respectively.


2. Internal Functions

An internal function is defined inside the main program or module.

Key Characteristics:

  1. Defined within the main program
  2. Can access variables from the enclosing scope (if host association is used)
  3. Useful for functions specific to one program
  4. Cannot be reused outside the program unless included in a module

Syntax of an Internal Function

program main
  ! Internal function is defined inside the program
  print *, "Square of 4:", square(4)

contains

  real function square(x)
real, intent(in) :: x
square = x**2
end function square end program main

Explanation:

  • The contains keyword indicates that functions defined below are internal to the program
  • square takes one input x and returns x**2

Output:

Square of 4: 16.0

3. External Functions

An external function is defined outside the main program, usually as a separate subprogram or in a separate file.

Key Characteristics:

  1. Defined independently of the main program
  2. Can be reused in multiple programs or modules
  3. Requires explicit declaration if used in another program
  4. Encourages modular programming

Syntax of an External Function

File: cube_function.f90

real function cube(x)
  real, intent(in) :: x
  cube = x**3
end function cube

File: main_program.f90

program main
  external :: cube
  print *, "Cube of 3:", cube(3)
end program main

Output:

Cube of 3: 27.0

Explanation:

  • The external statement informs the compiler that cube is defined outside the program
  • This function can be reused in any program by linking or including its file

4. Differences Between Internal and External Functions

FeatureInternal FunctionExternal Function
Definition LocationInside main program or moduleOutside the main program
ScopeLocal to the programCan be used in multiple programs
AccessibilityOnly within the programCan be reused elsewhere
ModularityLimitedHigh
Host Variable AccessYes (host association)No
Use of external keywordNot neededRequired

5. Advantages of Internal Functions

  1. Encapsulation: Keeps the function tied to one program
  2. Host association: Can use variables from the main program directly
  3. Simpler compilation: Defined in the same file as the main program

Example: Internal Function with Host Variables

program internal_example
  real :: factor
  factor = 2.0

  print *, "Double of 5:", multiply(5)

contains

  real function multiply(x)
real, intent(in) :: x
multiply = x * factor  ! Accesses variable from main program
end function multiply end program internal_example

Output:

Double of 5: 10.0

6. Advantages of External Functions

  1. Reusability: Can be used in multiple programs
  2. Modularity: Encourages separation of code into logical units
  3. Maintainability: Easier to update function without modifying main program
  4. Collaboration: Multiple programmers can work on different functions

Example: Reusing an External Function

File: square_function.f90

real function square(x)
  real, intent(in) :: x
  square = x**2
end function square

File: program1.f90

program program1
  external :: square
  print *, "Square of 6:", square(6)
end program program1

File: program2.f90

program program2
  external :: square
  print *, "Square of 8:", square(8)
end program program2

Output for program1:

Square of 6: 36.0

Output for program2:

Square of 8: 64.0

Explanation: The same external function square is reused in two programs.


7. Internal vs External Functions with Modules

Fortran modules can also be used to define reusable functions, combining benefits of internal and external approaches.

Example: Using a Module

File: math_module.f90

module math_module
contains
  real function cube(x)
real, intent(in) :: x
cube = x**3
end function cube end module math_module

File: main_program.f90

program main
  use math_module
  print *, "Cube of 4:", cube(4)
end program main

Output:

Cube of 4: 64.0

Advantages:

  • Combines reusability and encapsulation
  • No need for external statement
  • Provides a namespace for related functions

8. When to Use Internal Functions

  • The function is specific to one program
  • It needs access to host variables
  • Simpler projects where modularity is less critical

Example:

program internal_example2
  integer :: n
  n = 7
  print *, "Factorial of", n, "=", factorial(n)

contains

  integer function factorial(x)
integer, intent(in) :: x
integer :: i
factorial = 1
do i = 1, x
  factorial = factorial * i
end do
end function factorial end program internal_example2

Output:

Factorial of 7 = 5040

9. When to Use External Functions

  • The function will be reused in multiple programs
  • Code organization and modularity are important
  • The function does not need host variables

Example: External Function for Temperature Conversion

File: temp_convert.f90

real function celsius_to_fahrenheit(c)
  real, intent(in) :: c
  celsius_to_fahrenheit = c * 9.0 / 5.0 + 32.0
end function celsius_to_fahrenheit

File: main.f90

program main
  external :: celsius_to_fahrenheit
  print *, "100°C in Fahrenheit:", celsius_to_fahrenheit(100.0)
end program main

Output:

100°C in Fahrenheit: 212.0

10. Best Practices

  1. Use internal functions for simple, program-specific tasks
  2. Use external functions for reusable code across multiple programs
  3. Use modules for reusable functions with better organization
  4. Document function purpose, input, and output
  5. Avoid excessive reliance on host variables for clarity and maintainability

11. Advanced Applications

  • Internal Functions: Computations tied to a specific simulation or dataset
  • External Functions: Shared mathematical libraries, utility functions, I/O processing
  • Modules: Large scientific projects with multiple reusable functions

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *