MC, 2025
Ilustracja do artykułu: Fortran Hello World: The Perfect Starting Point for Beginners

Fortran Hello World: The Perfect Starting Point for Beginners

Fortran is one of the oldest programming languages in the world, but it’s still going strong in scientific computing and high-performance applications. If you’re just starting to learn Fortran, or if you’ve been curious about how to write your first program, then look no further! In this article, we’ll guide you through the process of writing a simple "Hello World" program in Fortran. This basic yet important program is the first step for many budding programmers. So let’s jump in and discover the magic of Fortran!

What is Fortran?

Before we dive into writing your first Fortran program, let’s take a quick look at what Fortran is and why it remains relevant today. Fortran, short for Formula Translation, was developed in the 1950s and was the first high-level programming language to be implemented. It’s primarily used for numeric computations and scientific work, making it highly popular in fields like physics, engineering, and computational biology. Fortran's ability to handle large datasets and perform complex calculations efficiently has ensured its place in modern supercomputing.

Why Start with "Hello World"?

“Hello World” programs are a traditional way for beginners to get their feet wet in a new programming language. The reason for this is simple: it’s a program that doesn’t require complex logic, and it outputs a message to the screen that confirms the program is running correctly. Writing and running a "Hello World" program helps you ensure that your development environment is set up properly, and it introduces you to the syntax and structure of the language.

Setting Up Your Development Environment

Before we start writing the program, let’s make sure your environment is ready for compiling and running Fortran code. You’ll need a Fortran compiler. Some popular Fortran compilers include:

  • GNU Fortran
  • Intel Fortran: A high-performance compiler used in professional and academic environments.
  • G95: Another open-source Fortran compiler.

Once you’ve chosen and installed your compiler, open a text editor to write your code, and you’ll be ready to go! Now let’s move on to writing your first Fortran program.

Writing the First Program: Fortran Hello World

The syntax for printing "Hello, World!" in Fortran is simple and concise. Here’s the minimal code you need to write your first Fortran program:

program hello_world
  print *, "Hello, World!"
end program hello_world

Let’s break down the components of this program:

  • program hello_world: This line declares the start of the program and names it "hello_world".
  • print *, "Hello, World!": This line is the heart of the program. It uses the print statement to output the text "Hello, World!" to the screen. The * is a formatting specifier, which tells Fortran to output the string as-is.
  • end program hello_world: This line marks the end of the program and is essential in Fortran to denote where the program logic finishes.

Once you’ve written this code, save it with a `.f90` extension (e.g., hello_world.f90) so the compiler can recognize it as Fortran 90 source code.

Compiling and Running the Program

Now that we have our code, it’s time to compile and run it! Open a terminal or command prompt, and navigate to the directory where your hello_world.f90 file is saved.

To compile the program using the GNU Fortran compiler (gfortran), run the following command:

gfortran hello_world.f90 -o hello_world

This will compile the program and create an executable named hello_world. To run the program, simply type:

./hello_world

You should see the output:

Hello, World!

Understanding the Output

If you see "Hello, World!" printed on the screen, congratulations—you’ve successfully written and run your first Fortran program! If there are any errors, they will usually point to syntax issues like missing keywords or incorrect punctuation. Don’t worry, this is a normal part of the learning process.

Fortran Hello World Examples with Modifications

Once you’ve mastered the basic "Hello World" program, you can modify it to experiment with different concepts. Below are a few variations that introduce additional functionality:

Example 1: Passing a Name as an Argument

In this example, we’ll modify the program to accept a user’s name as an input and greet them personally.

program hello_name
  character(len=20) :: name
  print *, "Enter your name:"
  read *, name
  print *, "Hello, ", trim(adjustl(name)), "!"
end program hello_name

Here, the program uses the read statement to take user input, stores it in a variable called name, and then prints a personalized greeting. The function trim(adjustl(name)) is used to remove any leading or trailing spaces from the input.

Example 2: Looping through an Array

For this variation, we’ll create an array and use a loop to print a greeting for each element of the array.

program hello_loop
  integer :: i
  character(len=20) :: names(3) = ['Alice', 'Bob', 'Charlie']
  do i = 1, 3
     print *, "Hello, ", trim(adjustl(names(i))), "!"
  end do
end program hello_loop

In this example, the program declares an array names with three elements. The do loop iterates over the array, printing a greeting for each name in the array.

Example 3: Conditional Greeting Based on Time of Day

This version of the "Hello World" program checks the current time and displays either "Good morning," "Good afternoon," or "Good evening" based on the time of day:

program hello_time
  integer :: hour
  print *, "Enter the current hour (0-23):"
  read *, hour
  if (hour < 12) then
     print *, "Good morning!"
  else if (hour < 18) then
     print *, "Good afternoon!"
  else
     print *, "Good evening!"
  end if
end program hello_time

In this example, the program asks the user to input the current hour and uses an if statement to determine the appropriate greeting based on the time of day.

Conclusion: Your First Step into the World of Fortran

Congratulations, you’ve just written your first Fortran program! As you can see, Fortran’s syntax is simple and easy to understand, and with just a few lines of code, you can create powerful applications. The "Hello World" program is just the beginning—there’s so much more you can do with Fortran, from performing complex calculations to creating simulations and analyzing large datasets.

We hope this article has helped you get started with Fortran programming. Whether you’re new to the language or looking to refresh your knowledge, writing "Hello World" is an excellent starting point. Keep experimenting, writing more programs, and learning as you go. The world of Fortran programming is waiting for you!

Komentarze (0) - Nikt jeszcze nie komentował - bądź pierwszy!

Imię:
Treść: