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

  1. Create a New File
  2. Write the Fortran Code
  3. Compile the Program
  4. Run the Program

  1. 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.

  1. 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 it HelloWorld.
  • 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 string Hello, World! to the console. The * indicates that the default format should be used.
  • end program HelloWorld: This line marks the end of the program.

  1. 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 -o hello_world hello_world.f90
  • 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 named hello_world.
  • hello_world.f90: The name of the Fortran source file to be compiled.

  1. Run the Program

After successfully compiling the program, you can run the executable file. In the terminal or command prompt, type the following command:

./hello_world

You should see the following output:

Hello, World!

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:

  1. Open the hello_world.f90 file in your text editor.

  2. Change the print statement to display a different message. For example:

    print *, 'Welcome to Fortran Programming!'
    
  3. Save the file.

  4. Recompile the program using the gfortran command:

    gfortran -o hello_world hello_world.f90
    
  5. Run the program again:

    ./hello_world
    

You should see the new message displayed in the console.

Common Mistakes and Tips

  • Forgetting implicit none: Always include implicit 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.

© Copyright 2024. All rights reserved