Reading function from a file in Fortran 90


I have an optimization solver in Fortran 90. So, if I want to change the objective function I have to modified the main file and write the objective function in this way:

subroutine fobj(n,x,f) implicit none integer :: n real(8) :: f real(8) :: x(n) intent(in ) :: n,x intent(out) :: f !OBJECTIVE FUNCTION f = x(1)**2-x(2)+2*x(3) end subroutine fobj

I have a big objective function, so I want to call this line "f = x(1)**2-x(2)+2*x(3)" from an external file or at least the subrutine.

Is that possible? (I'm new in Fortran.)

I know that I can modified the file with Python, but I want to do it in other file.

Thanks a lot!


Sure. Use:

include 'file.inc'

to include source code from an external file.


I'm not sure if this is what you're looking for, but:

Fortran also allows you to pass subroutine/function names around as actual arguments to subroutine/function calls. The corresponding dummy arguments must have the "external" attribute.

subroutine fobj(n,x,f,func) implicit none integer :: n real(8),external :: func real(8) :: f real(8) :: x(n) intent(in ) :: n,x intent(out) :: f !OBJECTIVE FUNCTION f=func(x,n) end subroutine fobj function func1(x,n) implicit none real(8) func1 integer n real(8) :: f,x(n) f = x(1)**2-x(2)+2*x(3) end function func1 function func2(x,n) implicit none real(8) func2 integer n real(8) :: f,x(n) f = x(1)**2+x(2)+2*x(3) end function func2 program main real(8),external :: func1,func2 real(8),allocatable :: x(:) real(8) :: f integer n n=50 allocate(x(n)) x=10. !Set X to a known value call fobj(n,x,f,func1) !Call func1 print*,f !10**2-10+2*10 = 110 x=10. !Reset X ... just to make sure there is no funny business in func1,func2 call fobj(n,x,f,func2) !Call func2 print*,f !10**2+10+2*10 = 130 deallocate(x) end program main

Of course, this program does nothing useful other than call func1 and func2 in obscure ways, but hopefully it illustrates the point. If you're looking to switch out the function at compile-time, then I think a include "myfile" is probably cleaner (just switching which file you're including at the time as suggested by @AlejandroLL)


You might also try to use Modules in your program. Sometimes when you pass special variables to your subroutines/functions you need to write interfaces for them. Using modules will improve your program structure and you'll be more effective and all interfaces would be generated automatically.


  • Add Jpanel to Jframe NetBeans
  • typescript: any difference between number[] and [number,number]?
  • detecting connection lost in spark streaming
  • WebApp in AppServices vs CloudService
  • How do i find all references to a user control
  • CUDA NSight is not installed with CUDA 5.0 installation file on Windows 8? [closed]
  • Netezza Incremental load from Sql server using SSIS
  • LNK1104: cannot open file 'kernel32.lib'
  • Regex for Specific Tag
  • Action Pack components in Rails
  • How can we prepend rows to a react native list-view?
  • import scipy.sparse failed
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • What's the purpose of QString?
  • Jackson Parser: ignore deserializing for type mismatch
  • Atlas images wrong size on iPad iOS 9
  • How to delay loading a property with linq to sql external mapping?
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • Counter field in MS Access, how to generate?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Javascript + PHP Encryption with pidCrypt
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Websockets service method fails during R startup
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Buffer size for converting unsigned long to string
  • Acquiring multiple attributes from .xml file in c#
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • Hits per day in Google Big Query
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Change div Background jquery
  • How does Linux kernel interrupt the application?
  • How to get Windows thread pool to call class member function?
  • How can I remove ASP.NET Designer.cs files?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why do underscore prefixed variables exist?
  • java string with new operator and a literal