Fortran 77 [F77] : Code in external file without subroutine?

In summary, the conversation discusses the possibility of injecting external code into a separate file without using a subroutine or function call. This is achieved by using the INCLUDE statement in standard Fortran 77 or by using the C preprocessor. This method is helpful for automating changes and avoiding the need to pass variables to another function. However, it is important to note that any changes to the included file will require recompilation of the program.
  • #1
Hepth
Gold Member
464
40
I have some F77 code and I am wondering if it is possible to inject external code into a separate file and call it without it being a subroutine, and placed into the code. An example would be :

a = 1 + 1
b = a + 1
c = b + a
a = c + b
d = 4 + a


and I want to take lines 3 and 4 and put them into somewhere else, say another file so I have

a = 1 + 1
b = a + 1
(externalfile.f)
d = 4 + a

and it has the same result.

So basically just HIDING code into another file, such that it will be evaluated inline without using a procedure or function call to it. It just uses the local variables that are in use.


The reason I do this is that I have a LOT of variables for a few results, and I am using the FortranForm from Mathematica. But it would be MUCH easier to automate this for changes if I can just export the code to a file and never really look at it, and then my main file wouldn't be ugly.

I really really want to avoid passing all these variables to another function.
 
Technology news on Phys.org
  • #2
Standard fortran 77 has an INCLUDE statement:

INCLUDE 'filename'

Most fortran 77 compilers have an option to use the C preprocessor (which can do more than just #include, of course).

I've no idea how this relates to Mathematica.
 
  • Like
Likes 1 person
  • #3
Thanks, for some reason I couldn't find that by searching what I wanted. Thats exactly it.
 
  • #4
Note that every time you change the "included" file, you have to recompile the program.
 
  • #5


Yes, it is possible to inject external code into a separate file and call it without it being a subroutine in Fortran 77. This can be achieved by using the INCLUDE statement.

The INCLUDE statement allows you to insert code from another file into your main code without using a subroutine or function. This can help in organizing your code and making it easier to manage.

For example, in your code, you can add the following line after line 2:

INCLUDE 'externalfile.f'

This will insert the code from the file 'externalfile.f' into your main code at that point. The code in the external file will be evaluated inline and will use the local variables that are in use.

However, it is important to note that using the INCLUDE statement can have an impact on the performance of your code. It is recommended to use it sparingly and only when necessary.

In conclusion, yes, it is possible to hide code in another file and have it evaluated inline without using a subroutine or function call in Fortran 77. You can use the INCLUDE statement to achieve this. Just make sure to use it carefully and consider its impact on the performance of your code.
 

Similar threads

Replies
3
Views
2K
Replies
8
Views
4K
Replies
25
Views
2K
Replies
4
Views
2K
Replies
2
Views
1K
Replies
12
Views
2K
Replies
59
Views
10K
Replies
5
Views
4K
Back
Top