In this section, we will guide you through writing your first Fortran program. By the end of this lesson, you will have a basic understanding of how to create, compile, and run a simple Fortran program.
Steps to Write Your First Fortran Program
- Create a New File
- Write the Fortran Code
- Compile the Program
- Run the Program
- Create a New File
First, open your text editor or Integrated Development Environment (IDE) and create a new file. Save this file with a .f90
extension, which is commonly used for Fortran 90 and later versions. For example, you can name it hello_world.f90
.
- Write the Fortran Code
Now, let's write a simple Fortran program that prints "Hello, World!" to the console. Here is the code:
program HelloWorld ! This is a simple Fortran program to print "Hello, World!" implicit none print *, 'Hello, World!' end program HelloWorld
Explanation of the Code
program HelloWorld
: This line defines the start of the program and names itHelloWorld
.implicit none
: This statement is used to ensure that all variables must be explicitly declared. It helps to avoid errors due to undeclared variables.print *, 'Hello, World!'
: This line prints the stringHello, World!
to the console. The*
indicates that the default format should be used.end program HelloWorld
: This line marks the end of the program.
- Compile the Program
To compile the Fortran program, you need a Fortran compiler. One of the most commonly used compilers is gfortran
, which is part of the GNU Compiler Collection (GCC).
Open your terminal or command prompt and navigate to the directory where you saved your hello_world.f90
file. Then, run the following command to compile the program:
gfortran
: The command to invoke the GNU Fortran compiler.-o hello_world
: This option specifies the name of the output executable file. In this case, it will be namedhello_world
.hello_world.f90
: The name of the Fortran source file to be compiled.
- Run the Program
After successfully compiling the program, you can run the executable file. In the terminal or command prompt, type the following command:
You should see the following output:
Congratulations! You have successfully written, compiled, and run your first Fortran program.
Practical Exercise
To reinforce what you've learned, try modifying the program to print a different message. Follow these steps:
-
Open the
hello_world.f90
file in your text editor. -
Change the
print
statement to display a different message. For example:print *, 'Welcome to Fortran Programming!'
-
Save the file.
-
Recompile the program using the
gfortran
command:gfortran -o hello_world hello_world.f90
-
Run the program again:
./hello_world
You should see the new message displayed in the console.
Common Mistakes and Tips
- Forgetting
implicit none
: Always includeimplicit none
to avoid errors related to undeclared variables. - File Extension: Ensure your file has the correct
.f90
extension. - Compilation Errors: If you encounter errors during compilation, carefully check your code for typos or syntax errors.
- Executable Permissions: On some systems, you may need to set execute permissions for the compiled program using
chmod +x hello_world
.
Summary
In this lesson, you learned how to write, compile, and run a simple Fortran program. You also practiced modifying the program to print a different message. These foundational skills will be essential as you progress through the course and tackle more complex Fortran programs.
Next, we will delve into the basic concepts of Fortran, starting with variables and data types.
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