Fortran FORTRAN95 Program for Iteration and Function Addition in PLATO IDE

  • Thread starter Thread starter dexter90
  • Start date Start date
  • Tags Tags
    Fortran95 Program
AI Thread Summary
The discussion revolves around issues in a Fortran program involving function and subroutine usage. The initial code attempts to call a function as if it were a subroutine, leading to confusion and errors. Key points include the incorrect use of the `call` statement for a function, which should be directly assigned to a variable. The function definition also contains a critical error where an expression is written instead of an assignment statement, which is a common mistake for those unfamiliar with Fortran syntax. Participants suggest removing the unnecessary `call` statement and emphasize the importance of understanding the distinction between functions and subroutines. They recommend studying Fortran basics to avoid such errors in the future. The conversation highlights the need for proper syntax and understanding of programming concepts to successfully compile and run Fortran code.
dexter90
Messages
14
Reaction score
0
Code:
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.
 
Technology news on Phys.org
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.
 
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/
 
Code:
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)
 
Dexter90:

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

Code:
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
 
I have solid fundation in programming. Your code not woking, I was writing the same code
 
o.k., I actually bother to compile now:
Code:
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.
 
Thank you,

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

I greet.
 
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.
 

Similar threads

Back
Top