Fortran Fortran 90/95: Read a Function?

Click For Summary
In Fortran, it is not possible to directly read a function input by the user at runtime, as Fortran is a compiled language. Users can pass function names as parameters, but these must be known at link time. To allow user-defined functions, a parser or interpreter must be implemented to convert user input into executable code. Alternatively, functions can be compiled at runtime, though this is more complex and less efficient compared to languages with built-in just-in-time compilation features. Overall, achieving dynamic function execution in Fortran requires additional tools or methods beyond standard programming practices.
NicolasPan
Messages
21
Reaction score
2
Hello everyone! I've been wondering if it is possible in Fortran to 'read' a function.For instance when I code this:
Fortran:
contains
function f(x)
   double precision :: f
   double precision ::x
   f=(whatever the function)
   return
end function
end program
It would be ideal if the program allowed the user to type the function each time, rather than having it pre-set by the programmer.Thanks in advance!
 
Technology news on Phys.org
Fortran is a compiled language. If you need this interpreting feature, you'll have to build a parser or something.
You can have function names as subroutine parameters (look up 'external'), but those functions have to be known and available at link time.
 
To expand on what BvU said, you don't "input" a function. What comes into your program is essentially text, so if the user types "sqrt", your program needs to parse this string to get its semantic meaning; i.e., that you want to call the sqrt() function. Although the string "sqrt" and the function name sqrt appear the same to us, they are very different as far as the program is concerned.
 
You have two options here.
1. You could use a parser/interpreter. Either write one yourself or use an existing one, e.g. http://fparser.sourceforge.net
2. If you need more performance you can also compile the function at runtime. That could for example be done by having your program send code through the Fortran compiler and then execute it. Of course languages with a built in just in time compiler like JavaScript would make this kind of thing a lot easier. JavaScript offers an "eval" function that can compile any code you give it at runtime making it execute a lot faster than any interpreter could.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
8K