FORTRAN 90

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Subprograms
Fortran 90 has two types of subprograms, functions and subroutines.
F
Fu
un
nc
cttiio
on
nss
Function are used by referring to their name (exactly as you would refer to
the intrinsic functions sin(x)) and they produce one answer. A fortran function,
or function subprogram, has the following syntax
type FUNCTION function-name (arg1, arg2, ..., argn)
IMPLICIT NONE
[specification part]
[execution part]
[subprogram part]
END FUNCTION function-name
type is a Fortran 90 type (e.g., INTEGER,REAL, LOGICAL, etc) , function
name is a Fortran 90 identifier , arg1, …, argn are formal arguments.
Somewhere in a function there has to be one or more assignment
statements like this:
function-name = expression
where the result of expression is saved to the name of the function.
ϭ
E
Ex
xa
am
mp
pllee
Write a program to read n integers number and then use an integer
function to calculate average of n numbers.
PROGRAM NUMBERS
IMPLICIT NONE
INTEGER::N,X,SUM
READ(*,*) N
DO I=1,N
READ(*,*) X
SUM=SUM+X
ENDDO
WRITE(*,50) AVERAGE(SUM,N
50 FORMAT(2X,'AVERAGE=',1X,i5)
END
INTEGER FUNCTION AVERAGE(SUM,N
IMPLICIT NONE
INTEGER::N,SUM
AVERAGE=SUM/N
END FUNCTION AVERAGE
E
Ex
xa
am
mp
pllee
Write a program to read n integers number and then use an integer
function to calculate the factorial of n.
program fact
implicit none
integer:: factorial,n
read*, n
write(*,7) factorial(n
Ϯ
7format (2x,i5
end
function factorial(n
implicit none
integer::fa,j,n,factorial
fa=1
do j=1,n
fa=fa*j
enddo
factorial=fa
return
end function factorial
S
Su
ub
br
ro
ou
uttiin
ne
ess
Subroutines are more general form of subprogram. They can perform
complicated tasks that return one or more answer. The syntax of a Fortran
subroutine is:
SUBROUTINE subroutine-name (arg1, arg2, ..., argn)
IMPLICIT NONE
[specification part]
[execution part]
[subprogram part]
END SUBROUTINE subroutine-name
Unlike functions, which can be used in expressions, subroutines can only
be called with the CALL statement. That means, the call to a subroutine must be
on its program line rather than somewhere in an expression. The following is the
syntax rules of the CALL statement:
CALL subroutine-name (arg1, arg2, ..., argn)
ϯ
E
Ex
xa
am
mp
pllee
Write a program to calculate cosine of angle with error less than 10-9. Use
subroutine program to calculate the factorial of the series.
Cos(x)=1-x2/2!+x4/4!-x6/6!+……
program cosine
implicit none
real,parameter::pi=3.14159,error=1e-9
integer::i=1,fact
real(kind=8)::sum=1.0,x,term
read(*,*)x
x=x*pi/180
do
call factorial(i,fact
term=(-1)**(i)*(x**(2*i))/(fact
sum=sum+term
if (abs(term)<error)exit
i=i+1
enddo
write(*,70) sum
70 format(2x,'cos(x)=',e15.1
end
subroutine factorial(i,fact)
integer::i,fact
fact=1
do j=1,2*i
fact=fact*j
enddo
end
ϰ
E
Ex
xa
am
mp
pllee
A class contains a number of student write a program with subroutine to
print the maximum degree and the name of student who take this degree. Use
subroutine to enter the student's names and degrees.
program student
implicit none
character (len=20)::name,name1
integer::degree,n,degree1
read(*,*)n
call best(n,name,degree,name1,degree1)
write(*,30) name1,degree1
30format( 2x,'name of student',2x,a,2x,'degree=',i3)
end
subroutine best(n,name,degree,name1,degree1)
implicit none
character(len=20)::name1,name
integer::degree1,i,degree,n
do i=1,n
read(*,*) name,degree
if(degree>degree1)then
degree1=degree
name1=name
endif
enddo
end
ϱ
Download