Q:

Need to read a file and assign its content to a function

I am a novice to Fortran 90 and stack overflow, and I have a simple question (I think).
I have a file, named Eq1, containing the following Fortran mathematical expression:
x**2
The objective is to input this expression from the file, and use it in a function inside fortran. I have tried the following
REAL FUNCTION  f(x)
    IMPLICIT  NONE
    REAL (kind =8), INTENT(IN) :: x

    open(1,file = 'Eq1',status='old')
        read(1,*) f
    close(1)
END FUNCTION  f

and when I do
print *, f(0.1d+0)

inside the main program, I get the following error message:
Fortran runtime error: Bad real number in item 1 of list input

Thanks for the help!
Cheers

A:

Fortran does not have a string evaluation functionality as some script languages, e.g. Python's eval().
You have to write your own string to expression parser, which is not a trivial task.
One simple workaround is to call Python's eval() with SYSTEM and use it's output: 
REAL FUNCTION  f(x)
    IMPLICIT  NONE
    REAL (kind =8), INTENT(IN) :: x
    CHARACTER(LEN=200) EXP
    CHARACTER(LEN=400) CMD
    open(1,file = 'Eq1',status='old')
        read(1,*) exp
    close(1)
    WRITE(CMD, "(A,f14.7, A, A, A)") 'python -c "x=',x,';print ',trim(exp), '" > out.txt'
    CALL system(CMD)
    open(2,file = 'out.txt',status='old')
        read(2,*) f
    close(2)
END FUNCTION  f

program calc
  implicit none
  real f
  print *, f(0.1d+0)      
  print *, f(2.0d+0)
  print *, f(3.0d+0)
  print *, f(4.0d+0)
end program calc

Output:
9.9999998E-03
4.000000
9.000000
16.00000

