1 / 9

Fortran- Subprograms

Fortran- Subprograms. Chapters 6, 7 in your Fortran book. Statement Functions. There may be a simple calculation that is used multiple times that is not an intrinsic function in Fortran If the computation can be written in a single assignment statement, we can use a statement function:

doris
Download Presentation

Fortran- Subprograms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fortran-Subprograms Chapters 6, 7 in your Fortran book

  2. Statement Functions • There may be a simple calculation that is used multiple times that is not an intrinsic function in Fortran • If the computation can be written in a single assignment statement, we can use a statement function: Function name (argument list) = expression • Statement functions are placed at the beginning of the program, preceding any executable statements. • The function name and its arguments should be included in the type statement or it will be decided implicitly (integer or real based on first letter) ex: REAL CYVOL,HEIGHT,RAD CYVOL(RAD,HEIGHT)=3.14*RAD*HEIGHT

  3. Notes on Functions • Functions compute a single value • Functions can never be used on the left side of an equal sign in an assignment statement • The output of the function is determined intrinsically (I-N is an integer) or it must be specified in the type statement • The arguments must be enclosed in parentheses • The arguments may be constants, variables, expressions, or other functions

  4. Subprograms • Sometimes the same calculations must be used several times in a program or for different programs. Instead of writing the steps out each time, a subprogram can be added to the program that is referenced to by the main program. • Subprograms make debugging programs simpler, increases readability of the program, and can be used multiple times and in different programs. • Fortran has two types of subprograms: functions and subroutines

  5. User-Defined Functions • A function subprogram is like a program of its own that, instead of beginning with PROGRAM, begins with FUNCTION: FUNCTION name (argument list) and concludes with a RETURN statement preceding END that directs the program back to the main program • The arguments in the main program must match the dummy arguments in the subprogram in type, number, and order • If one of the arguments is an array, its dimensions must be specified in the main program and the subprogram • Function subprograms return a single value that is stored in the function name • A function can contain references to other functions, but not to itself • The subprogram can be positioned before or after the main program or in a different file • The same variable names can be used in the main program and subprogram without causing confusion • The function name should be listed in the type statement in the main program and the subprogram

  6. Example • Here is an example of a program with a function subprogram: PROGRAM TEST INTEGER TEST1, TEST2, TEST3, AVE, TAVE READ*, TEST1, TEST2, TEST3 TAVE=AVE(TEST1,TEST2,TEST3) PRINT*, TAVE END ************************************* REAL FUNCTION AVE(A,B,C) REAL A,B,C AVE=(A+B+C)/3 RETURN END

  7. Subroutines • The subroutine is referenced from the main program with the following statement: CALL subroutine name (argument list) • This statement will reference the subroutine that has the first line consisting of: SUBROUTINE subroutine name (argument list) • There are a number of rules regarding writing and using subroutines: • The name of the subroutine has nothing to do with the type of data (real or integer) associated with the subroutine. • The first line of the subroutine identifies it as a subroutine, assigns a name that will be used to reference the subroutine, and also contains a list of arguments. • The list of arguments is necessary to transfer data and variables (input) to the subroutine as well as returning values to the calling program. The arguments that are used in the CALL statement are the actual arguments while the arguments in the SUBROUTINE statement are the dummy arguments. The arguments in the CALL statement must match in type, number and order those used in the subroutine definition. The actual variable names do not have to match, however if the first variable in the CALL statement is a real array, the first dummy argument in the SUBROUTINE statement must also be a real array. • A subroutine may return one or more values, or no values. • Variables used within a subroutine are therefore independent of variables used in the main program or in other subroutines. Any variables that are used in the subroutine that are not arguments are local variables, and the values are not accessible from the main program. Therefore if we have a variable named LENGTH in the main program, and the value of LENGTH is not transferred in the list of arguments, we can also have a variable LENGTH in the subroutine that is independent of the variable in the main program.

  8. Subroutine Rules, continued • Be very careful with the dimension statements for multidimensional arrays in subroutines. You should always pass the dimensional size [DIMENSION A(100,100)], and the size which actually used [NROWS, NCOLS] as arguments. • For example: Say that we have an array A in the main program that has been dimensioned:   DIMENSION A(10,10) • But we only have NCOLS=2, and NROWS=3 which are being used. If we have a subroutine SORT which we are transferring the array A in:   CALL SORT (A,NCOLS,NROWS) • In the subroutine we have (note the dummy arguments in the subroutine use NC for NCOLS and NR for NROWS) SUBROUTINE SORT(A,NC,NR) DIMENSION A(NR,NC) . . RETURN END • Values in an array are actually stored in columns. When we dimension an array 10 x 10 we are reserving 100 locations in memory for A. The dimension statement in the SUBROUTINE SORT is only obtaining the first 6 (NC x NR) values from memory. Since the array is only stored in columns, we only be getting the first 6 values from column 1. To avoid this you should always provide the same array dimensioning in the main program as well as in subroutines.

  9. Subroutine Rules, continued • Subroutines require a RETURN statement to return control to the main program or another subroutine that calls it. An END statement is also required. • A subroutine may reference other subroutines, however it cannot reference itself.

More Related