innlooki.blogg.se

Simply fortran graphics examples
Simply fortran graphics examples





  1. #Simply fortran graphics examples how to
  2. #Simply fortran graphics examples free

If y had been given the SAVE attribute or declared with a SAVE statement, y would be a static variable meaning that only one copy of y would be used for all calls to sub1. This is the proper behavior for a recursive routine.

simply fortran graphics examples

The variable y is local to the subroutine and a private copy of y is generated each time sub1 is called. module module1 integer:: n contains recursive subroutine sub1(x) integer, intent(inout):: x integer:: y y = 0 if (x < n) then x = x + 1 y = x**2 print *, 'x = ', x, ', y = ', y call sub1(x) print *, 'x = ', x, ', y = ', y end if end subroutine sub1 end module module1 program main use module1 integer:: x = 0 print *, 'Enter number of repeats' read (*,*) n call sub1(x) end program mainExecuting this program with n = 5 produces the following output: x = 1, y = 1 x = 2, y = 4 x = 3, y = 9 x = 4, y = 16 x = 5, y = 25 x = 5, y = 25 x = 5, y = 16 x = 5, y = 9 x = 5, y = 4 x = 5, y = 1The variable x is an argument of the subroutine and retains the value stored by the fifth call of sub1. The following routine shows how local variables behave in a recursive Fortran 90 routine. In other words, each call of the routine should have its own private copy of all local variables that are not declared with the SAVE statement. In normal recursive routines, local variables should be automatic and not static. One must be careful, however, when writing recursive routines that use local variables. Not to mention having to write math functions like sin( ) and cos( ) to handle such data types. You have to manually define a complex data type. So stick that in your complex variables, C programmers, and - oh wait, I forgot there are no complex variables in C. You would think they might know something about it by now!) But this example shows that even in FORTRAN 77 one can quickly and easily write routines that are recursive. When they say Fortran, they mean FORTRAN 77, since they absolutely refuse to acknowledge the existence of modern Fortran 90. module module1 integer:: n contains recursive subroutine sub1(x) integer, intent(inout):: x if (x Ĭ programmers love to gloat that recursion can not be done in Fortran.

#Simply fortran graphics examples free

Here's the same program coded in modern Fortran 90: (Note Fortran 90 can be written in free form and need not start in column 7). Subroutines and functions may call themselves only if they are explicitly declared with the recursive keyword. For N = 5, the following output is produced: x = 1 x = 2 x = 3 x = 4 x = 5įortran 90 (and 95), on the other hand, supports recursion directly. This routine simply asks the user how many times to make SUB1 call itself, and then each time SUB1 is executed it increments X and prints the value after each increment. In fact, on every system I've tried so far, this routine works perfectly. N) THEN X = X + 1 PRINT *, 'x = ', X CALL DUMSUB(X,DUMSUB) END IF ENDI've read in some texts that indirect reference is not allowed, but ask yourself "How can SUB1 possibly know that it's actually calling itself when it calls the dummy subroutine?" It can't, and thus this must work. PROGRAM MAIN INTEGER N, X EXTERNAL SUB1 COMMON /GLOBALS/ N X = 0 PRINT *, 'Enter number of repeats' READ (*,*) N CALL SUB1(X,SUB1) END SUBROUTINE SUB1(X,DUMSUB) INTEGER N, X EXTERNAL DUMSUB COMMON /GLOBALS/ N IF(X. The subroutine can then call itself by calling the dummy subroutine. However, one can implement recursion in a round about way by passing the subroutine as an argument to itself. Attempting to do such will result in a compile time error with compilers that adhere to the FORTRAN 77 standard.

simply fortran graphics examples simply fortran graphics examples

FORTRAN 77 subroutines and functions are not allowed to call themselves directly.

#Simply fortran graphics examples how to

Example 1: How to write recursive routines in FORTRAN 77Įxample 2: Behavior of local variables in recursive routines in FORTRAN 77Įxample 3: Use of the DATA statement in recursive routines in FORTRAN 77Įxample 4: Summary of local variable behavior on several platforms.Įxample 1: Recursive Routines in FORTRAN 77 (and Fortran 90)First let me say that I think every serious Fortran programmer should always write new code in Fortran 90 or Fortran 95, but for those of you stuck on a desert island with only a FORTRAN 77 compiler at your disposal, here's a way to write a recursive subroutine.







Simply fortran graphics examples