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

Simple program in FORTRAN95

  1. May 20, 2012 #1
    Code (Text):
    program iteration
    real :: p
    IMPLICIT NONE
    call add(5)
    p=add(5)

    end iteration

    real function add(a)
    real :: a
    a+2
    end function add
    It not working... Why?? I use PLATO IDE ( FORTRAN 95 )

    I greet.
     
  2. jcsd
  3. May 20, 2012 #2
    What happens when you run it? Does it produce an error?

    I don't think you need to include the line call add(5), since add() is a function (and not a subroutine). The assignment statement below (p=add(5)) is sufficient.

    Try removing the line call add(5), and please provide the output or error message if it still does not work.
     
  4. May 20, 2012 #3

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    There are so many things wrong here (at least five) that it suggests either you don't know Fortran at all, or you don't know how to interpret the messages you got when you tried to compile the code.


    The best answer to either problem is find a Fortran tutorial and work through it. This uses PLATO and Silverfrost: http://www.fortrantutorial.com/
     
  5. May 20, 2012 #4
    Code (Text):
    program iteration

    IMPLICIT NONE
    call potega(5)


    end iteration

    real function potega(a)
    real :: a
    a+2
    end function potega
    And I have error:

    Error: Statement not recognised
    Warning: The result of FUNCTION POTEGA has never been set
    Warning: n a call to POTEGA from another procedure, the first argument was of type INTEGER(KIND=3), it is now REAL(KIND=1)
     
  6. May 20, 2012 #5
    Dexter90:

    You really need to study just the basics...programming without knowing the syntax is just a guessing game.

    Code (Text):

    program iteration

    IMPLICIT NONE
    real::a
    a = potega(5)
    write(*,*) a
    end iteration

    real function potega(a)
    real :: a
    potega = a+2
    end function potega
     
     
  7. May 21, 2012 #6
    I have solid fundation in programming. Your code not woking, I was writing the same code
     
  8. May 21, 2012 #7
    o.k., I actually bother to compile now:
    Code (Text):

    program iteration

    IMPLICIT NONE
    real::a, potega
    a = potega(5.0)
    write(*,*) a
    end program iteration

    real function potega(a)
    real :: a
    potega = a+2
    end function potega
     
    Besides, I never said you did not have a solid foundation in progarmming, it just does not look like you are bothering to put a basic effort on reading fortran when you call the same function as a function as a subroutine in the same program.

    Good luck.
     
  9. May 21, 2012 #8
    Thank you,

    I was reading that the fortran functions are placed at the end, My mistake. I am sorry.

    I greet.
     
  10. May 21, 2012 #9

    Mark44

    Staff: Mentor

    Fortran distinguishes between FUNCTIONs and SUBROUTINEs. Your code mixed these two ideas, as you declared add/potega as a function, but used it as if it were a subroutine.

    The other major mistake that I saw was the expression 'a + 2' in the function body. Writing an expression rather than an assignment statement makes me question how solid your programming foundation is. That is not the kind of mistake that someone with experience makes.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Simple program in FORTRAN95
  1. An simple program (Replies: 3)

  2. Fortran95 HELP (Replies: 5)

Loading...