Download presentation
Presentation is loading. Please wait.
Published byTobias Hicks Modified over 9 years ago
1
ReAl :: x OK CHARACTER :: name OK, a 1 character name! CHARACTER(LEN=10) :: name OK, string length 10 REAL :: var-1 cannot have -1 in a declaration var_1 would be INTEGER :: 1a Cannot start a variable with a number. BOOLEAN :: loji No BOOLEAN type, use LOGICAL. CHARACTER(LEN=5) :: town = "Glasgow" OK(ish), town is set to Glasg. CHARACTER(LEN=*), PARAMETER :: city = "Glasgow“
2
LOGICAL :: wibble =.TRUE. OK CHARACTER(LEN=*), PARAMETER :: "Bognor“ Error, no variable name REAL, PARAMETER :: pye = 22.0/7.0 OK REAL :: a = 1., b = 2 OK, the value 2 is promoted to be 2.0 LOGICAL(LEN=12) :: frisnet Error cannot use LEN=12 with LOGICAL. CHARACTER(LEN=6) :: you_know = 'y'know" Unbalanced delimiters ' and ", the second ' will cause an error too. CHARACTER(LEN=6) :: you_know = "y'know"
3
http://www.liv.ac.uk/HPC/HTMLF90Course/ HTMLF90CourseQuestionsnode22.html
4
CHAPTER 9 SUBPROGRAMS Subprograms are useful in the following cases: long complicated program Subprograms make it readable and simple by dividing it into smaller programs. set of operations is to be repeated Subprograms are used to avoid repetition.
5
FORTRAN language has two types of subprograms: FUNCTIONS: return single value. SUBROUTINES: return more than one value. 1) INTRINSIC functions example: ConstantSIN (10.0) VariableSIN (theta) ExpressionSIN (2.0*alfa + theta/3.0) Other functionSIN (ABS (a-b)) Functions: There are two types of functions:
6
2) USER-DEFINED functions Example: PROGRAM STATMENT_FUNCTION IMPLICIT NONE INTEGER,DIMENSION(3,4)::RENT INTEGER :: SET,I,J,L,K SET(K,L) = K +L/3 DO I=1,3 DO J=1,4 RENT(I,J)=SET(I,J) END DO PRINT*,'ELEMENTS OF ARRAY RENT ARE:' DO I=1,3 PRINT*,(RENT(I,J),J=1,4) END DO STOP END PROGRAM STATMENT_FUNCTION
7
External Function Subprogram External function subprogram can be made accessible to a program unit by attaching it after the END statement of the main program. EXAMPLE Write a main program to read three values and prints their average. The average values should be calculated using an external function subprogram. PROGRAM Test REAL :: Test 1, Test 2, Test 3, Ave READ *, Test 1, Test 2, Test 3 PRINT 5, Ave (Test 1, Test 2, Test 3) 5FORMAT (1X, ‘Average=’, F5.2) STOP END PROGRAM Test REAL FUNCTION Ave (X, Y, Z) REAL :: X, Y, Z Ave=(X+Y+Z)/3.0 RETURN END FUNCTION Ave
8
EXAMPLE : Follow the execution of the following program and show its output? PROGRAM FUNCTION_SUBPROGR AMS IMPLICIT NONE REAL :: A,B,C,TIME,ADD A = 1.0 B = 2.0 C = 3.0 TIME = 2.0*ADD(A,B,C) PRINT 10, TIME 10 FORMAT (1X, 'TIME=', F6.2) STOP END PROGRAM FUNCTION_SUBPROG RAMS ! ! FUNCTION SUBPROGRAM REAL FUNCTION ADD(X,Y,Z) REAL :: X,Y,Z Y = X +Y Z = Y + Z X = Z + X ADD = X + Y + Z RETURN END FUNCTION AD
9
Subroutine Subprogram Subroutines are modulus written to perform operations that cannot be performed by a function. For example, if several values need to be returned from a module, a sub-routine is used. A subroutine is also used for operations that do not compute values, such as reading the values in a data file. Subroutines may be either internal or external. External Subroutines Subprogram External subroutines can be made accessible to a program unit by attaching it after the END statement of the main program.
10
Subroutines Rules 1.A subroutine does not represent a value; thus, its name should be chosen for documentation purposes and not to specify a real or integer value. 2.A subroutine is referenced with an executable statement whose general form is CALL subroutine name (argument list) 3.The first line in a subroutine identifies it and includes the subroutine name and argument list, as follows: SUBROUTINE name (argument list) 4.A subroutine uses the argument list for subroutine inputs and values returned to the calling program. arguments in CALL statement actual arguments. arguments in SUBROUTINE dummy arguments. The arguments in the CALL statement must match in type, number, and order those used in the subroutine definition.
11
5.A subroutine may return one value, many values, or no value. Similarly, a subroutine may have one input value, many input values, or no input value. 6.Arguments are the link between the main program and the subroutine. Thus, the choice of subroutine statement numbers and variable names is independent of those in the main program. Variables used in subroutine and not subroutine arguments local variables their values are inaccessible from main program. 7. The subroutine, like the function, requires a RETURN statement to return control to main program.It also requires an END statement. 8.A subroutine may reference other functions or call other subroutines, but it cannot call itself.
12
EXAMPLE: Write a Fortran program which uses a subroutine program to calculate the average, the minimum value and the maximum value of an array X with number of elements N represents the scores of students grade. PROGRAM Scores INTEGER :: N, I REAL :: Tests (100), Ave,Min,Max READ*, N READ*, (Tests(I), I=I,N) CALL Stat (Tests, N, Ave, Min, Max) Print 5, Ave, Min, Max 5Format (1X, 3F10.3) END PROGRAM Scores SUBROUTINE Stat (X, M, Xave, Xmin, Xmax) INTEGER :: M, I REAL :: X(M), Xave, Xmin, Xmax, Sum Sum=X(1) Xmax=X(1) Xmin=X(1) DO I=2,M Sum=Sum+X(I) IF (X(I) < Xmin) Xmin=X(I) IF (X(I) > Xmax) Xmax=X(I) END DO Xave=Sum/M RETURN END SUBROUTINE Stat
13
EXAMPLE: Rent A Car! Program Rented_Car_cost Integer::Dis Real::Total,Average Character(20)::Name, Model Read 2, Name, Model,Dis 2 Format(2(A20,/),I6) Call Rent(Dis,Total,Average) Print 5,"Customer Name:",Name,'Car Model:',Model 5 Format(2x,2(2A20,/)) Print 10,'Travel Distance (km):',Dis,'Total Rent (SR):',Total 10 Format(2x,A22,I6,/,A20,F7.2) Stop End Program subroutine Rent(D,TR,Avg) Integer::D Real::TR,Avg If (D.LE.100) THEN TR= D*0.5 Else if (D.LE.300) THEN TR= 100*0.5+(D-100)*0.3 Else TR= 100*0.5+200*0.3+(D- 300)*0.2 End if Avg=TR/ D end subroutine Rent
14
EXAMPLE Follow the execution of the following program and show its output? PROGRAM SUBROUTINE_EXAMPLE IMPLICIT NONE INTEGER,DIMENSION (10):: K INTEGER::I DO I=1,10 K(I)=I END DO CALL MODIFY (K,10) PRINT*,"NEW VALUES OF K ARE:" PRINT 15, (K(I),I=1,10) 15 FORMAT(1X,5I5) END PROGRAM SUBROUTINE_EXAMPLE SUBROUTINE MODIFY(L,N) INTEGER :: I,N INTEGER, DIMENSION (N):: L DO I=1,N L(I)=MOD(L(I),4) END DO RETURN END SUBROUTINE MODIFY
15
PROGRAM MAIN_ABC Read*, A,B ANSWER= C(A,B) WRITE(6,100)‘C= ',ANSWER 100 FORMAT(1X,A3,F10.2) END !END OF PROGRAM MAIN_ABC !BEGINNING OF FUNCTION C FUNCTION C(x,y) C= SQRT(x**2 + y**2) RETURN END !END OF FUNCTION C EXAMPLE: Write a program that calculate the following: Use a function subprogram
16
implicit none integer :: N, square, cube print *, "Please type an integer and press RETURN" read *, N call square_and_cube( N, square, cube ) print * print *, N, " squared is ", square print * print *, N, " cubed is ", cube end subroutine square_and_cube( i, i_squared, i_cubed ) implicit none integer, intent(in) :: i integer, intent(out) :: i_squared, i_cubed i_squared = i**2 i_cubed = i**3 end subroutine EXAMPLE: Write a subroutine that calculate the sequare and cube of any number.
17
Exercise: Write a complete FORTRAN 90 program to convert temperatures on the Celsius scale to the corresponding temperatures on the Fahrenheit scale according to the relationship: Fahrenheit = 1.8*Celsius + 32
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.