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:
- Defined within the main program
- Can access variables from the enclosing scope (if
host associationis used) - Useful for functions specific to one program
- 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
containskeyword indicates that functions defined below are internal to the program squaretakes one inputxand returnsx**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:
- Defined independently of the main program
- Can be reused in multiple programs or modules
- Requires explicit declaration if used in another program
- 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
externalstatement informs the compiler thatcubeis 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
| Feature | Internal Function | External Function |
|---|---|---|
| Definition Location | Inside main program or module | Outside the main program |
| Scope | Local to the program | Can be used in multiple programs |
| Accessibility | Only within the program | Can be reused elsewhere |
| Modularity | Limited | High |
| Host Variable Access | Yes (host association) | No |
Use of external keyword | Not needed | Required |
5. Advantages of Internal Functions
- Encapsulation: Keeps the function tied to one program
- Host association: Can use variables from the main program directly
- 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
- Reusability: Can be used in multiple programs
- Modularity: Encourages separation of code into logical units
- Maintainability: Easier to update function without modifying main program
- 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
externalstatement - 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
- Use internal functions for simple, program-specific tasks
- Use external functions for reusable code across multiple programs
- Use modules for reusable functions with better organization
- Document function purpose, input, and output
- 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
Leave a Reply