Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum in f90 not working?

Status
Not open for further replies.

xrayspex1

Technical User
Jun 10, 2008
9
SI
hi all, i'm a complete newbie to fortran - if someone can take 30 seconds to look at the short code below and tell me why it prints the wrong sum of two numbers, i'd be very gratefull, because i'm freaking out already...

CODE:
program temp
implicit none

real,external :: add
real :: y

y = add(3,4)
print *,y

end program temp

real function add(x,n)
implicit none
real, intent(in) :: x
real, intent(in) :: n

add = x+n

end function add

OUTPUT:
9.8090893E-45
Press any key to continue

any ideas?????
 
It is because there is no prototyping in Fortran so it doesn't really know what the parameter types are. Try
Code:
   y = add (3.0, 4.0)
Reason why you're getting 9.8E-45 is because 3 and 4 are combined into one floating point number. The other one is just a random number on the stack.
 
Nice one Arkm - I'd completely forgotten about INTERFACE.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top