Subroutines in Fortran are a fundamental concept that allows you to encapsulate and reuse code. They are similar to functions in other programming languages but are used primarily for performing tasks rather than returning values. This section will cover the basics of subroutines, how to define and call them, and best practices for using them effectively.

Key Concepts

  1. Definition of Subroutines: A subroutine is a block of code that performs a specific task and can be called from different parts of a program.
  2. Calling Subroutines: How to invoke a subroutine from the main program or other subroutines.
  3. Passing Arguments: How to pass data to and from subroutines.
  4. Scope and Lifetime: Understanding the scope and lifetime of variables within subroutines.

Defining a Subroutine

A subroutine is defined using the subroutine keyword followed by the subroutine name and an optional list of arguments. The subroutine ends with the end subroutine statement.

Syntax

subroutine subroutine_name(argument_list)
    ! Declarations
    ! Executable statements
end subroutine subroutine_name

Example

Let's define a simple subroutine that prints a greeting message.

program main
    call greet()
end program main

subroutine greet()
    print *, "Hello, welcome to Fortran programming!"
end subroutine greet

Explanation

  • program main: The main program block.
  • call greet(): Calls the greet subroutine.
  • subroutine greet(): Defines the greet subroutine.
  • print *, "Hello, welcome to Fortran programming!": Prints a greeting message.
  • end subroutine greet: Ends the greet subroutine.

Passing Arguments to Subroutines

Arguments can be passed to subroutines to make them more flexible and reusable. Arguments are specified in the subroutine definition and the call statement.

Example

Let's modify the previous example to pass a name to the greet subroutine.

program main
    call greet("Alice")
end program main

subroutine greet(name)
    character(len=*), intent(in) :: name
    print *, "Hello, ", name, "! Welcome to Fortran programming!"
end subroutine greet

Explanation

  • call greet("Alice"): Calls the greet subroutine with the argument "Alice".
  • subroutine greet(name): Defines the greet subroutine with an argument name.
  • character(len=*), intent(in) :: name: Declares name as a character string input argument.
  • print *, "Hello, ", name, "! Welcome to Fortran programming!": Prints a personalized greeting message.

Practical Exercise

Exercise

Write a subroutine called add_numbers that takes two integers as input arguments, adds them, and prints the result.

Solution

program main
    call add_numbers(5, 7)
end program main

subroutine add_numbers(a, b)
    integer, intent(in) :: a, b
    integer :: sum
    sum = a + b
    print *, "The sum of ", a, " and ", b, " is ", sum
end subroutine add_numbers

Explanation

  • call add_numbers(5, 7): Calls the add_numbers subroutine with the arguments 5 and 7.
  • subroutine add_numbers(a, b): Defines the add_numbers subroutine with two integer arguments a and b.
  • integer, intent(in) :: a, b: Declares a and b as integer input arguments.
  • integer :: sum: Declares a local variable sum to store the result.
  • sum = a + b: Calculates the sum of a and b.
  • print *, "The sum of ", a, " and ", b, " is ", sum: Prints the result.

Common Mistakes and Tips

  • Forgetting to Declare Arguments: Always declare the arguments in the subroutine.
  • Mismatched Argument Types: Ensure that the types of arguments in the call statement match those in the subroutine definition.
  • Intent Attribute: Use the intent attribute to specify whether an argument is for input (intent(in)), output (intent(out)), or both (intent(inout)).

Conclusion

In this section, you learned how to define and use subroutines in Fortran. Subroutines help in organizing code, making it more modular and reusable. You also learned how to pass arguments to subroutines and handle them within the subroutine. Practice writing your own subroutines to become more comfortable with this powerful feature of Fortran.

Next, we will explore functions in Fortran, which are similar to subroutines but are used to return values.

© Copyright 2024. All rights reserved