! The ! symbol is used for comments. Comments should be used in your ! program where appropriate. Any text after the ! is ignored by the ! compiler. ! Spaces are also ignored by the compiler (except between the ' '). ! When you save your program, you should give it the .f90 extension. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!! REMEMBER, REMEMBER, REMEMBER, REMEMBER !!! The book uses single precion and not double precsion !!! This means that we have to declare floating point variables !!! differently and we need to be careful about how we !!! specify constants that have digits after the decimal point !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!! ! This is a MODULE ! A module can be used for many things, but for the time being, we ! will use it to declare some commonly needed global variables so we ! don't have to keep defining them over and over. ! ! The module 'lives' outside the body of the main program. !!!!!!!!!!!!!!!!!!!!! MODULE const ! ! All global constants should be given the PARAMETER attribute. This ! prevents the value of the global constant from being changed. ! ! The module can be contained in the same .f90 file as the main program ! or in a separate .f90 file. We will include it in the main program for ! now. ! INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0) REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0 END MODULE const ! The main program starts below !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !! Begin the header section of the main program !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! PROGRAM triangle ! PROGRAM keyword indicates start of main program ! The name of your program must follow the same rules as any other ! variable. It cannot be a reserved word and cannot be the ! same name as some other variable in your program USE const ! USE keyword inserts the contents of the const module into your ! program. ! This is similar to the include statement in other languages. IMPLICIT NONE ! This directive forces all variables to be explicitly declared. ! Prior to F90, Fortran used Implicit Typing. This means that ! any variable starting with ! ! i,j,k,l,m,n = automatically integer ! any other letter = automatically single precision real ! ! unless specificly declared otherwise. ! ! This was necessary to save memory, which was very expensive ! at the time. ! ! Implicit typing is a very bad programming practice so we are going ! to use this to enforce good programming practices ! This is a simple program to compute the area of a triangle given ! the base and height ! ! Declare the variables for the program. Here, we will need 3 floating point ! variables (ie, double precision variables). These can be declared all on ! one line as done below or each variable can be on its own line. REAL(KIND=dp) :: base,height,area !!!!! ! This is the end of the header section and beginning of the executable statement ! section !!!!! ! Prompt the user for the input variables. You can do this using 2 ! separate READ statements as done below, or use a single READ statement. ! The syntax for a single READ statement would be: ! READ(*,*) base, height WRITE(*,*) 'Input the base' READ(*,*) base WRITE(*,*) 'Input the height' READ(*,*) height ! Compute the area area = base*height/2 ! Echo (or copy) the value of the area variable to the screen ! This is the same as printing the value of the variable to the screen WRITE(*,*) 'The area of the triangle is = ',area !!!!! ! This is the end of the executable statement section and the beginning of the end ! section !!!!! ! The end section consists of the single statement below END PROGRAM triangle !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ! Sample Runs ! Input the base ! 1.0d0 ! Input the height ! 6.0d0 ! The area of the triangle is = 3.00000000000000 ! ! Input the base ! 0.5d0 ! Input the height ! 10.0d0 ! The area of the triangle is = 2.50000000000000