![]() In GNU Fortran, single precision real type handles factorials of integers as large as 34, and double precision as large as 170. To handle larger integers, the types can be changed to real or double precision. The above factorial subprogram, with variables of integer type, works only for nonnegative integers no larger than 12, as 13! = 6,227,020,800 exceeds the Fortran upper limit of 2,147,483,647 for integers. The entire function subprogram appears in the source file after the final end statement of the main program. In Fortran, function subprograms do not have to be declared as they do in Basic. (Above it is the line "fact = p".) The subprogram concludes with an end statement. Somewhere in the subprogram there must be a line giving the value of the function. The subprogram has its own type statements, declaring the type of the function itself, as well as the types of the variables involved in computing the function. The first line of the function subprogram specifies the name of the function, and lists in parentheses the variables upon which the function depends. Here is a function subprogram defining the factorial function, fact(n) = n! : More precise formatting can be accomplished with double loops and tab indicators.įunction subprograms in Fortran define functions too complicated to describe in one line. Here is one example demonstrating how to print a matrix A having 5 rows and 6 columns of real numbers, with each row of the matrix printed on its own line : There are various formatting tricks useful in printing two-dimensional arrays. Prints only the weights indexed from p to q. Prints the seven entries of weights to the screen, while the statement If you want to input say only the first five weights, you can do so with the statement The user can either enter the seven weights one-by-one separated by returns, or alternatively, can enter all seven weights separated only by commas, and then a single return. ![]() This read statement pauses the program to allow the user to enter all seven entries of the array. For instance, the above array weights can be input with only the statement ![]() But you can sometimes more efficiently do the same with single statements. Instructs Fortran to set aside storage space for a list of at most 100 names, each a string of length no longer than 30 symbols, as well as a list of at most 100 scores, each a real number.Īs in Basic, in Fortran you may input and print arrays with do loops. Here is one way to introduce a real array weights, indexed from 1 to 7:īut the same can be accomplished more briefly with the single statementĪlthough the upper and lower limits of an array cannot be variables, they can be constants declared in parameter statements. If you use a dimension statement to declare an array, you should precede it with a type declaration. (In this respect Fortran is less flexible than Basic, in that Basic allows the dimension of an array to be a variable whose value can be input by the user, thereby ensuring that exactly the right amount of storage space is reserved.) You don't have to use the full size of the array specified in the declaration statements that is, you may reserve space for more entries in the array than you will need. A colon separates the lower and upper limits whenever both are specified.īecause arrays are declared at the beginning of the program, they must be given a fixed size - i.e., the limits must be constants rather than variables. In Fortran the default lower limit of the range of a subscript is 1, rather than 0 as in Basic. Here are examples of arrays introduced by type declarations: The latter way is preferred, because it is best anyway to declare the type of the array. Arrays can be declared with either a dimension statement or a type declaration. Array declarations in Fortran go at the beginning of the program, before any executable statement. There are only a few minor differences in the way Fortran and Basic treat arrays.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |