Transmitting values to functions and subroutines problems - Fortran 90

  • Context: Fortran 
  • Thread starter Thread starter Martin_Caceres
  • Start date Start date
  • Tags Tags
    Fortran Functions
Click For Summary
SUMMARY

The discussion focuses on the challenges of transmitting values between functions and subroutines in Fortran 90 when integrating with the simulation software PRO/II. The user, David, is attempting to modify existing Fortran code to allow direct input from PRO/II, but encounters issues with variable accessibility across multiple functions and subroutines. Solutions proposed include utilizing the CONTAINS statement to define functions and subroutines within the main subroutine, ensuring that they can access shared variables. Additionally, the importance of using MODULES for better variable management is highlighted.

PREREQUISITES
  • Understanding of Fortran 90 programming language
  • Familiarity with subroutines and functions in Fortran
  • Knowledge of the PRO/II simulation software
  • Experience with variable scope and data sharing in programming
NEXT STEPS
  • Research the use of the CONTAINS statement in Fortran 90 for better variable accessibility
  • Learn about MODULES in Fortran to manage shared data effectively
  • Explore error handling for LNK4006 messages in Fortran
  • Investigate optimization techniques for reducing execution time in Fortran simulations
USEFUL FOR

This discussion is beneficial for Fortran developers, simulation software integrators, and anyone working on optimizing data flow between functions and subroutines in complex programming environments.

Martin_Caceres
Messages
2
Reaction score
0
Hi everyone, I hope everyone's doing good. I presently have a project at work and I'm having a hard time dealing with some programming techniques.

To summarize my project, I have to modify some codes on Fortran so that it can be adapted to be used on a simulation software called PRO/II. All the functions and subroutines have already been written.

However, to make the codes compatible with PRO/II, I have to change the way of assigning some input data (input by the user himself) on Fortran. In fact, before, the user entered the data on a text file which was then read by a fortran subroutine.

However, now, the data is input in the simulation software directly. I managed to write a code to record all the input data in a subroutine. But when the simulation is ran on PRO/II, it only attributes the input data to the "MAIN SUBROUTINE". The values are not accessible to any functions or subroutines outside the main subroutine. In fact, PRO/II gives the values to the arguments of my main subroutine only.

As from there, when a function is called from the main subroutine, there's no problem. It's the function that calls other functions or subroutines that are the issues. I'll try to make myself as clear as possible. So let's say I have a subroutine X and many functions and subroutines as follows:

Subroutine X


End Subroutine


Function A(variables)

Uses Functions B and C

End Function

Function B(variables)

Uses Function D and E

End Function

Function C(variables)

Uses functions D and E

End Function

Function D(variables)

End function

Function D(variables)

End Function

Function E(variables)

End Function

So, the problem is that the values I calculated in my main subroutine or the values I input in PRO/II which are transmitted to the Fortran program are not accessible to functions D and E. So, I tried copying all the values needed to a text file from the main subroutine and reading all the values each time by the different functions and subroutines. But it's taking forever for the simulation to run by PRO/II. I have like 80 functions and 20 subroutines, and each time they are called, they open the text file to read the values.

Is there a way for me to have the values read by all functions and subroutines without having to read from the text file? In other words, is there a way to make all the variables I've calculated in my main subroutine to every function and subroutine in my program?

I'm really having a hard time figuring that out.

If you guys don't understand the problem or have any questions, please let me know.

Thanks in advance for your help guys.

David
 
Technology news on Phys.org
Depending on how your Fortran program is structured, you have a couple of choices.

If the values you need are already available in your MAIN, all you may need to do, now, is to move your functions into MAIN under a CONTAINS clause:

Code:
program MAIN

  ! main 
  ! read input
  ! make function calls
  .
  .
  .
contains

  subroutine A
  .
  .
  .
  end subroutine A
 
  function B
  .
  .
  .
  end function B

  function C
  .
  .
  .
  end function C

end program MAIN

Just make sure you do not have conflict in variable names.

Give this a try; also, you should read about MODULES.
 
Hi gsal,

First of all, I'd like to thank you for your prompt response.

Actually, I don't have a main program, it's more like a main subroutine. The PRO/II software kinda acts like a main program. I execute everything from there.

I kinda already tried the CONTAINS statement a few weeks ago. For example, if my program has the following structure, can the functions communicate between them in the same CONTAINS statement?



Subroutine Main

Calls Subroutine A


CONTAINS


SUBROUTINE A

CALLS SUBROUTINE B

CALLS SUBROUTINE C


END SUBROUTINE A


SUBROUTINE B

USES FUNCTION A

END SUBROUTINE B


FUNCTION A

USES FUNCTION B

END FUNCTION A


FUNCTION B

USES FUNCTION C

USES FUNCTION D

END FUNCTION B


FUNCTION C

.
.
.
.

END FUNCTION C


FUNCTION D

.
.
.
.

END FUNCTION D


END SUBROUTINE MAIN


I hope it's not too confusing. So, this is a really simplified version of my whole program. My issue lies within the link between the subroutines and the functions. For example the subroutine A has access to the values of my main, but when it calls subroutine B which in turn uses function A which in turn uses function B, are all those inter-communication and calls work within a CONTAINS statement?

Also, when I tried the CONTAINS statement today, I got many LNK4006 error messages. Does it mean that I have to rename all my functions and subroutines? I thought the scope of the function or subroutine was limited to itself?

As I previously mentionned, I tried to copy all the data I needed to a text file from the main program and reading it every time by all the functions and subroutines. But with this method, instead of taking 20 minutes, it takes 3 whole days to execute.

Again, thanks a lot for helping me out, it's greatly appreciated.

David
 

Similar threads

  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K