Passing variables between subroutines in different files using Fortran

In summary: I hope this answers your question since it worked for me. I run my FORTRAN code on Snow Leopard. Now I have multiple subroutines in different files that I call in my main program. The way I made it work was to do: gfortran -o mainExec mainprogram.f file1.f file2.f ... fileN.fSo mainprogram.f is obviously the main program. file1.f has some number of subroutines while file2.f has some other number of different subroutines etc. Once you then run the executable (mainExec) everything should be fine. Hope that helps. When including multiple files, look up
  • #1
SheldonM
1
0
Good day,

I'm modifying a Monte Carlo program (MCCCS Towhee) using Fortran and am having trouble passing an argument (a 2x1 array) from a subroutine withing one file (the existing main loop that 'drives' the code) to another subroutine written in another file (an energy calculation, also part of the existing program package). I've written another subroutine (call it 'AAA') in the main loop that receives the array, assigns it to another variable name (same type, size); then, in the other file, I call AAA to receive the array. The code compiles and runs fine until the call to the subroutine is made, and then it exits with a segmentation fault error (which is strange because I've checked and rechecked the type declarations - they seem fine). I'm a newbie to Fortran, so I'm probably not seeing a very simple solution. Since this program has a sophisticated configure and Makefile setup, and if the solution to my problem means that I must create a new *.F file, what changes (if any) must be be made to the files associated with configure and make?

Many thanks,
Sheldon
 
Technology news on Phys.org
  • #2
Hi Sheldon-

I hope this answers your question since it worked for me. I run my FORTRAN code on Snow Leopard. Now I have multiple subroutines in different files that I call in my main program. The way I made it work was to do:

gfortran -o mainExec mainprogram.f file1.f file2.f ...

So mainprogram.f is obviously the main program. file1.f has some number of subroutines while file2.f has some other number of different subroutines etc. Once you then run the executable (mainExec) everything should be fine. Hope that helps.
 
  • #3
When including multiple files, look up MODULES for Fortran. However, I believe you can only use them in Fortran 90 and up. Either way, they make it easier to allow the programmer to package subroutines in a common file. There are also a few different things such as interfaces which allow for even greater functionality.

As smigs said, in your Makefile, each file needs to be compiled with the correct dependencies with the -c flag, and then the final executable, linking the objects with the -o flag.
 
  • #4
check with -ffree-form when compiling.
 
  • #5


Hello Sheldon,

It sounds like you are on the right track with creating a new subroutine to receive the array and then calling it in the other file. However, the issue may be with the type declarations as you mentioned. It is important to make sure that the types of the variables match in both subroutines, as well as their sizes. Also, make sure that the variable is properly initialized before being passed to the other subroutine.

As for the changes to the configure and Makefile setup, it will depend on the specific program and how it is set up. It is best to consult the program's documentation or reach out to the developer for guidance on how to properly add a new file to the program.

Additionally, it may be helpful to use a debugger to track down the source of the segmentation fault. This can help pinpoint where the issue is occurring and allow for a more targeted solution.

Best of luck with your modifications to the program!
 

1. How do I pass variables between subroutines in different files using Fortran?

To pass variables between subroutines in different files using Fortran, you can use the "use" statement to access the variables declared in another file. You can also use the "common" statement to share variables between different files.

2. Can I pass an array between subroutines in different files using Fortran?

Yes, you can pass arrays between subroutines in different files using Fortran. You can use the "use" statement to access the array declared in another file or use the "common" statement to share the array between different files.

3. Is there a limit to the number of variables I can pass between subroutines in different files using Fortran?

There is no limit to the number of variables you can pass between subroutines in different files using Fortran. However, it is recommended to keep the number of variables to a minimum for better code organization and readability.

4. Can I modify a variable in one subroutine and have the changes reflected in another subroutine in a different file?

Yes, you can modify a variable in one subroutine and have the changes reflected in another subroutine in a different file. This can be achieved by using the "common" statement to share the variable between different files.

5. Are there any potential issues I should be aware of when passing variables between subroutines in different files using Fortran?

One potential issue to be aware of when passing variables between subroutines in different files using Fortran is variable name conflicts. If two files have variables with the same name, it can cause errors or unexpected behavior. To avoid this, it is recommended to use unique variable names or use the "rename" option in the "use" statement to give the variable a different name in each file.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top