Download presentation
Presentation is loading. Please wait.
1
mdul-cntns-sub-cmmnts2.f90
Interactive Commenting
2
Part 1 Module sub ! illustrates some non-executables and a subroutine in global module implicit none save ! ensure that values assigned to variables in module sub are retained integer, parameter :: n=5 ! non-executable: assign a value to a variable in a module integer :: i real, dimension (n) :: f,x,g contains ! ‘contains’ is method to include a subroutine Subroutine sign_chg(a) ! note use of underscore in a name is allowed real :: a(:) ! assumed shape array, it be one dimensional size specified by use. a = -a ! can use assign to change value of itself, even in this array arithmetic return ! optional ; note: can have multiple places to return from within a sub. end subroutine sign_chg end module sub ! end of the module, everything above is in this module Program testy2
3
Part 2 Program testy2 use sub ! insert info. in module sub; comes first, before implicit none in main prog. implicit none real :: pi, z(2*n) ! Alt. use of parameter in module; defines array z, twice size of n ! end of non-executables, executables follow (comment, not executed) pi = acos(-1.) ! a simple way to define pi Z = 1. ! fill array elements of z with the value 1. Call sign_chg(z) Print *, (z(2*i-1), i=1,n) ! print odd values of z x = (/(i*0.5, i=1,n)/) ! assign values to x with implied do loop in a data statement: / … / do i=1,n f(i) = x(i)**2 ! Use integer (not real) if power is int.; faster & avoids a type of error end do g=x**2 ! array arithmetic, calculates whole array g same result as for array f call printy(pi) ! illustrates calling a user-supplied subroutine call sign_chg(f) ! calls subroutine in the module that flips sign of f call printy(3.) ! passes pointer to dummy location where value 3. has been placed end program testy2
4
Part 3 end program testy2 subroutine printy(r) ! illustrates example of user-supplied subroutine use sub ! insert the information in the module (comes first, before implicit none) real :: r ! local variable definition (note: it is passed in subroutine argument list) print *, 'Printy input = ',r print *,' i x f g' ! simple spacing for column labels do i=1,n ! note that i and n are global variables passed here via module sub print 2,i,x(i),f(i),g(i) ! arrays x, f, and g passed via module sub end do print *, 'again using implied do loop' print 1 1 format(2x,'i',5x,'x',10x,'f',10x,'g') ! more clever spacing for column labels print 2,(i,x(i),f(i),g(i), i=1,n) ! another use of implied do loop in an I/O statement 2 format(i3,3f10.5) end subroutine printy
5
Output ./a.out Printy input = i x f g again using implied do loop i x f g Printy input =
6
Part 1 Module sub ! illustrates some non-executables and a subroutine in global module implicit none save ! ensure that values assigned to variables in module sub are retained integer, parameter :: n=5 ! non-executable: assign a value to a variable in a module integer :: i real, dimension (n) :: f,x,g contains ! ‘contains’ is method to include a subroutine Subroutine sign_chg(a) ! note use of underscore in a name is allowed real :: a(:) ! assumed shape array, it be one dimensional size specified by use. a = -a ! can use assign to change value of itself, even in this array arithmetic return ! optional ; note: can have multiple places to return from within a sub. end subroutine sign_chg end module sub ! end of the module, everything above is in this module Program testy2
7
Part 2 Program testy2 use sub ! insert info. in module sub; comes first, before implicit none in main prog. implicit none real :: pi, z(2*n) ! Alt. use of parameter in module; defines array z, twice size of n ! end of non-executables, executables follow (comment, not executed) pi = acos(-1.) ! a simple way to define pi Z = 1. ! fill array elements of z with the value 1. Call sign_chg(z) Print *, (z(2*i-1), i=1,n) ! print odd values of z x = (/(i*0.5, i=1,n)/) ! assign values to x with implied do loop in a data statement: / … / do i=1,n f(i) = x(i)**2 ! Use integer (not real) if power is int.; faster & avoids a type of error end do g=x**2 ! array arithmetic, calculates whole array g same result as for array f call printy(pi) ! illustrates calling a user-supplied subroutine call sign_chg(f) ! calls subroutine in the module that flips sign of f call printy(3.) ! passes pointer to dummy location where value 3. has been placed end program testy2
8
Part 3 end program testy2 subroutine printy(r) ! illustrates example of user-supplied subroutine use sub ! insert the information in the module (comes first, before implicit none) real :: r ! local variable definition (note: it is passed in subroutine argument list) print *, 'Printy input = ',r print *,' i x f g' ! simple spacing for column labels do i=1,n ! note that i and n are global variables passed here via module sub print 2,i,x(i),f(i),g(i) ! arrays x, f, and g passed via module sub end do print *, 'again using implied do loop' print 1 1 format(2x,'i',5x,'x',10x,'f',10x,'g') ! more clever spacing for column labels print 2,(i,x(i),f(i),g(i), i=1,n) ! another use of implied do loop in an I/O statement 2 format(i3,3f10.5) end subroutine printy
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.