Introduction to Fortran

Simple Program Construction in Fortran » Running Subroutines

Did you notice that we didn’t really gather any weather information? We just put it in there magically. We don’t have the skills to be able to gather that data in this lesson, but let’s suggest a structure useful for making this happen. Because gathering weather data can be thought of as a different process than picking out clothing, we can separate the programs as two separate subroutines.

So let’s think of the weather data grab as a subroutine. We aren’t going to code the weather data grab subroutine, we will just assume the subprogram returns the high temperature variable to the main program. The high temperature variable is “passed” into the subroutine on the “call” line below.

Main Program Content

 integer high_temp  print *, “Before subroutine -- High temperature forecast is: “ print *, high_temp  call weather_data_grab(high_temp)  print *, “After subroutine -- High temperature forecast is: “ print *, high_temp 

Subroutine Content

 subroutine weather_data_grab (htemp) integer htemp    .    .                .             return end 

The subroutine has to be named the same as the call from the main program. However, it is best to name the variable differently in the subroutine than in the main program as there can be confusion between the two if they are named exactly the same way. The important part of the subroutine is the return statement which ensures that the main program gets the passed in variables back. This means the subroutine can change the value of the passed in variables and return them to the main program with different values than what they started as.

Question 1 of 1

If the above code was our entire program, how many lines of output would be printed to screen?

The correct answer is d.

There will only be 4 lines of output from the program. You may have been tempted to say there were only 3 because one of the lines won’t “output” anything visibly, but in reality it is outputting a line break, you just can’t “see” it. Here is what the output will look like.

 Before subroutine -- High temperature forecast is:  After subroutine -- High temperature forecast is: 64 
Please make a selection.

Subroutines are common in larger programs as each “problem” will be separated into separate subroutines. So knowing how subroutines are called and variables are returned helps you know how to navigate around the subroutines to find where your data is flowing.

In our Fortran program, we can now replace the “high_temp = 64” line with the subroutine call from above like so:

 program pick_outfit implicit none  integer high_temp logical weekend integer wardrobe(8) integer outfit(8) integer i, j  !The first four elements in the array are for tops !wardrobe(1) = short-sleeved top !wardrobe(2) = long-sleeved dress top !wardrobe(3) = t-shirt !wardrobe(4) = long-sleeved top  !the last four elements in the array are for bottoms !wardrobe(5) = shorts !wardrobe(6) = pants !wardrobe(7) = athletic shorts !wardrobe(8) = pajama pants  call weather_data_grab(high_temp)  weekend = .TRUE. wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /) !outfit not set yet, so just setting it to all zeroes for safety outfit = (/ 0, 0, 0, 0, 0, 0, 0, 0 /)  if (weekend .EQ. .TRUE.) then      if (high_temp .GE. 70) then           outfit(3) = 1      else           outfit(4) = 1      end if      if (high_temp .GE. 60) then           outfit(7) = 1      else           outfit(8) = 1      end if else      if (high_temp .GE. 70) then           outfit(1) = 1      else           outfit(2) = 1      end if      if (high_temp .GE. 60) then           outfit(5) = 1      else           outfit(6) = 1      end if end if  wardrobe = wardrobe - outfit  do i = 1, 8      if (outfit(i) .EQ. 1) then           print *, clothing_type(i)      end if end do  do j = 1, 8      if (wardrobe(j) .EQ. 0) then           print *, “Do laundry today!”      end if end do  end