I want make a function as I type and read it as a function. For example, if I type x+y, then f(x,y)=x+y. Is this possible? The following code does not work.
real function f(x,y)
real x,y
write(6,*) "type f(x,y)"
read*, f
return
end
Yes, you can, but your syntax is a bit off.
PROGRAM READFUNC
REAL x,y,F,res
res = F(x,y)
WRITE(*,*) res
END
REAL FUNCTION F(x,y)
REAL x,y
WRITE (*,*) "Type in"
READ (*,*) F
RETURN
END
note that I compiled this w/ gfortran so I'm not sure if it uses any F90+ extensions or not.
EDIT After reading your edits, I see that this isn't what you want; you want some kind of eval/parser. In general this is not a trivial thing. You're going to have to do some kind of token parsing work.
However there are libraries that can do this for you that are already written.
See this article for an example of where to look for more research.
F(X,Y)=X+Y
99 READ(*,*)X,Y
WRITE(*,*)F(X,Y)
GO TO 99
You enter 3 4 or 3,4 and you get out 7
f
be ignoring thex
andy
passed to it?