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
- 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.
- Calling Subroutines: How to invoke a subroutine from the main program or other subroutines.
- Passing Arguments: How to pass data to and from subroutines.
- 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 thegreet
subroutine.subroutine greet()
: Defines thegreet
subroutine.print *, "Hello, welcome to Fortran programming!"
: Prints a greeting message.end subroutine greet
: Ends thegreet
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 thegreet
subroutine with the argument"Alice"
.subroutine greet(name)
: Defines thegreet
subroutine with an argumentname
.character(len=*), intent(in) :: name
: Declaresname
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 theadd_numbers
subroutine with the arguments5
and7
.subroutine add_numbers(a, b)
: Defines theadd_numbers
subroutine with two integer argumentsa
andb
.integer, intent(in) :: a, b
: Declaresa
andb
as integer input arguments.integer :: sum
: Declares a local variablesum
to store the result.sum = a + b
: Calculates the sum ofa
andb
.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.
Fortran Programming Course
Module 1: Introduction to Fortran
- Introduction to Fortran
- Setting Up the Development Environment
- Basic Syntax and Structure
- Writing Your First Fortran Program
Module 2: Basic Concepts
- Variables and Data Types
- Operators and Expressions
- Input and Output
- Control Structures: If Statements
- Control Structures: Loops
Module 3: Arrays and Strings
Module 4: Procedures and Functions
Module 5: Advanced Data Structures
Module 6: File Handling
Module 7: Advanced Topics
Module 8: Best Practices and Optimization
- Code Optimization Techniques
- Debugging and Profiling
- Writing Maintainable Code
- Fortran Standards and Portability