Fortran 90/95: Read a Function?

Click For Summary

Discussion Overview

The discussion revolves around the possibility of allowing users to input a function in Fortran 90/95 at runtime, rather than having it predefined in the code. The scope includes programming techniques, language capabilities, and potential workarounds for dynamic function evaluation.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant inquires about the feasibility of reading a function in Fortran, expressing a desire for user-defined input rather than hardcoded functions.
  • Another participant notes that Fortran is a compiled language and suggests that an interpreting feature would require building a parser.
  • A further contribution clarifies that inputting a function involves parsing text input to derive its semantic meaning, emphasizing the distinction between user input and programmatic function calls.
  • Options are presented for achieving dynamic function evaluation, including writing a parser or interpreter, or compiling functions at runtime, with a comparison to languages like JavaScript that support runtime code execution more easily.

Areas of Agreement / Disagreement

Participants generally agree that Fortran does not support direct user input of functions in the way described, and multiple competing views on potential solutions and workarounds are presented.

Contextual Notes

Limitations include the need for a parser to interpret user input and the constraints of Fortran as a compiled language, which affects how functions can be dynamically handled.

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.
 

Similar threads

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