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.
 

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

What is Fortran 77?

Fortran 77 (F77) is a programming language used for scientific and engineering applications. It was one of the first high-level languages developed for numerical and scientific computing in the 1950s. It has since been updated and evolved, but F77 remains a widely used language in the scientific community.

What is the purpose of using external files in F77?

External files in F77 allow for the separation of code from data. This means that the code can be written and stored separately from the data, making it easier to manage and modify. It also allows for easier collaboration between multiple programmers working on the same project.

Can F77 code be written without using subroutines?

Yes, F77 code can be written without using subroutines. However, using subroutines allows for code modularity and reusability, making it easier to maintain and update the code in the long run.

How do you include an external file in F77 code?

To include an external file in F77, you can use the "include" statement followed by the name of the file enclosed in quotes. This will insert the contents of the external file into the main F77 code at the location of the include statement.

Are there any limitations to using external files in F77?

One limitation of using external files in F77 is the potential for errors if the included file is not properly formatted or if there are conflicting variable names between the main code and the included file. It is important to carefully check and test the code to ensure that all included files are functioning correctly.

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