Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Simple fortran error

  1. Sep 30, 2008 #1
    I am trying to write a fortran program to take an integer n and print out the first n rows of Pascal's triangle. The code I have so far:

    PROGRAM PascalTriangle
    IMPLICIT NONE
    INTEGER i, n, j, ktemp, ktemp1, ktemp2, ktemp3, ktemp4, ktemp5
    WRITE(6,*), "Enter a positive integer n:> "

    READ(5,*) n

    do 10 i = 1, n
    do 20 j = 1, i
    ktemp1 = FACT(i)
    ktemp2 = FACT(j)
    ktemp3 = i - j
    ktemp4 = FACT(ktemp3)
    ktemp = ktemp1 / ktemp2 * ktemp4
    write(*), ktemp
    20 continue
    10 continue
    STOP
    END

    FUNCTION FACT(N)
    FACT=1
    DO 30 J=2,N
    FACT=FACT*J
    30 CONTINUE
    RETURN
    END

    Whats really confusing me is that the compiler throws a single error at the ktemp1 = FACT(i) line, stating that its an invalid declaration of or reference to symbol FACT. It looks like it should be a simple fix but I can't seem to get around the error. Any help is greatly appreciated.
     
  2. jcsd
  3. Sep 30, 2008 #2

    uart

    User Avatar
    Science Advisor

    Try using a local variable to hold the intermediate values of "fact" and only assigning it to the functions return value once at the completion of the function.


    Code (Text):

    function fact(n)
    tmpfact :: integer
    tmpfact = 1
    do 30 j-2,n
        tmpfact = tmpfact * j
    30 continue
    fact=tmpfact
    return
     
     
  4. Sep 30, 2008 #3

    jtbell

    User Avatar

    Staff: Mentor

    Try including the type of data being returned, in the function definition:

    INTEGER FUNCTION FACT(N)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Simple fortran error
  1. Fortran error (Replies: 33)

  2. [FORTRAN] Runtime error (Replies: 12)

Loading...