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

Fortran output has lots of errors that I don't understand

  1. Jun 9, 2013 #1
    I am trying to write a program that in Fortran 95 will integrate x^2 from 0 to 1. Easy right?? I'm getting errors, though, that I really don't understand. Here is my code:

    program montecarlo2
    implicit none
    real :: fmax,iseed,srand,xmax,x2,x,y
    !why do we need pi??
    integer :: icount,nmax,ans0
    iseed = 329048234
    call srand(iseed)

    nmax = 10000

    !integrate x^2 from zero to one
    icount = 0
    xmax = 1
    fmax = x2(xmax)
    ans0 = 0.
    do i=1,nmax
    x = rand()
    y = rand()
    f = x2(x)
    if(y.le.f/fmax) then
    icount = icount+1
    end if
    ans0 = ans0 + x2(y)
    end do
    print *,dfloat(icount)/dfloat(nmax),ans0/dfloat(nmax)

    function x2(x)
    implicit none
    x2 = x**2
    end function x2

    and here is the output:


    function x2(x)
    Error: Unclassifiable statement at (1)

    implicit none
    Error: Unexpected IMPLICIT NONE statement at (1)

    x2 = x**2
    Error: 'x2' at (1) is not a variable

    end function x2
    Error: Expecting END DO statement at (1)
    Error: Unexpected end of file in 'montecarlo2.f95'

    please help!!
  2. jcsd
  3. Jun 9, 2013 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    You probably should have and END or END PROGRAM statement after the PRINT statement. You have not instructed the compiler that there are no more statements in the main program 'montecarlo2'. When the compiler sees a function definition, then it becomes confused.
    Last edited: Jun 9, 2013
  4. Jun 9, 2013 #3
    Thank you, @SteamKing! It worked!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook