vortibp.blogg.se

Simply fortran tutorial
Simply fortran tutorial




(Although the user does not input z, GNU Fortran will have given it some default value.) After the subroutine does its calculations, the new values of a, b, c are substituted back into the main program for x, y, z. When the subroutine is called it substitutes x for a, y for b, and z for c.

simply fortran tutorial

Print *, "What are the two numbers you want to average?" Here is a simple program named average that prompts the user for two real numbers, calls a subroutine named avg to average the numbers, and then prints the average. (Oversights lead to type-mismatch error messages by the compiler.) The variables in the call statement must match the variables in the subroutine according to number, type, and dimension. The subroutine executes, modifying some or all of its variables, which are then substituted back for the original call variables in the main program. The call statement has also a list of variables, which are substituted for the subroutine variables. The main program uses a call statement to call the subroutine. A subroutine begins with variable declaration statements, just as the main program. A variable may be of any type, including a character variable, and can be an array. A subroutine must have a name, followed by a list of variables in parentheses. Subroutines are typed in the source file after the main program. Ī subroutine in Fortran works like a subprogram in Basic, except that you do not declare a subroutine. In Fortran the expression mod(n,m) gives the remainder when n is divided by m it is meant to be applied mainly to integers. (For programs with a large number of variables a more efficient method is to use DATA statements we will discuss these later.) The easiest way to do this is with ordinary assignment statements, such as "x = 0", or "y = 2.61", etc. If you are worried about the problem, you can assign values to all your variables at the beginning of your program - a procedure called "initializing variables". But trouble might arise if a forgetful programmer proceeds on the assumption that the default value is zero, or perhaps neglects to include an assignment statement. This strange behavior is hardly ever a problem, as usually when the variable is eventually used in the program it is given an appropriate value by some assignment statement. A real variable with no value specified will be given a value - but usually a very small value that is not precisely zero, and sometimes a value that is not even close to zero. In GNU Fortran the situation is more confused.

simply fortran tutorial

Recall that in Basic the default value of a numeric variable is always zero - that is, if you introduce a numeric variable but do not specify its value, Basic automatically gives it the value zero.






Simply fortran tutorial