Fortran 90/95: Read a Function?

In summary, the conversation discusses the possibility and methods of allowing users to input functions in Fortran, a compiled language. This would require a parser or interpreter to interpret the function as text and determine its semantic meaning. Alternatively, the function could be compiled at runtime for better performance.
  • #1
NicolasPan
21
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 

1. What is Fortran 90/95 and how is it different from previous versions?

Fortran 90/95 is a programming language used for scientific and numerical computing. It is an update to the original Fortran language, and introduced many new features such as modules, dynamic memory allocation, and built-in array operations, making it more modern and efficient.

2. How do I read a function in Fortran 90/95?

To read a function in Fortran 90/95, you can use the "read" statement followed by the function name and any necessary variables. For example, "read(5) my_function(x,y)" will read the values of x and y and pass them to the function my_function.

3. Can I use a function as an argument in Fortran 90/95?

Yes, Fortran 90/95 allows you to pass functions as arguments to other functions. This is useful for creating more complex and flexible programs. To do this, you can declare the argument as "external" in the function declaration and pass the function name as an argument when calling the function.

4. How do I handle errors when reading a function in Fortran 90/95?

You can use the "iostat" keyword in the read statement to check for errors when reading a function. It will return a value indicating if the read was successful or if there was an error. You can also use the "err" keyword to specify an error message to be displayed if an error occurs.

5. Can I use Fortran 90/95 to read functions from external files?

Yes, Fortran 90/95 allows you to read functions from external files using the "include" statement. This allows you to store your functions in separate files for better organization and easier editing. You can then include the file containing the function using the "include" statement before your main program.

Similar threads

  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top