Transmitting values to functions and subroutines problems - Fortran 90

In summary: If the functions and subroutines are in different modules, you would need to use a MODULE statement in your Fortran code. In summary, David is trying to solve a problem where he needs to access the values of variables in his main subroutine from other functions and subroutines in his program, but he is not able to do so because the values are not accessible from outside the main subroutine.
  • #1
Martin_Caceres
2
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
  • #2
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.
 
  • #3
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
 

FAQ: Transmitting values to functions and subroutines problems - Fortran 90

What is the difference between passing values by reference and passing values by value in Fortran 90?

When passing values by reference, the memory address of the variable is passed to the function or subroutine. This means that any changes made to the variable within the function will also affect the original variable in the calling program. On the other hand, when passing values by value, a copy of the variable's value is passed to the function, so any changes made to the variable within the function will not affect the original variable in the calling program.

How do you pass an array as an argument to a Fortran 90 subroutine?

To pass an array as an argument, you need to specify the dimensions of the array in both the subroutine header and the calling program. Then, within the subroutine, you can access the array elements using the dummy array argument. It is important to note that Fortran 90 passes arrays by reference, so any changes made to the array within the subroutine will also affect the original array in the calling program.

Can you use the "intent" attribute when passing arguments to Fortran 90 subroutines?

Yes, the "intent" attribute can be used to specify whether a dummy argument is used for input, output, or both. This can help with debugging and ensuring that the subroutine is not accidentally changing the values of variables in the calling program.

How do you handle optional arguments in Fortran 90 subroutines?

In Fortran 90, you can use the "optional" keyword in the subroutine header to specify that an argument is optional. Then, in the calling program, you can use the "present" function to check if the optional argument was included. If it was not included, you can assign a default value to the argument within the subroutine.

What is the difference between a function and a subroutine in Fortran 90?

A function is a subprogram that returns a single value, while a subroutine can perform multiple actions and does not return a value. Additionally, functions can be used in expressions, while subroutines cannot. In Fortran 90, functions are declared with the "function" keyword and subroutines with the "subroutine" keyword.

Similar threads

Replies
59
Views
10K
Replies
8
Views
4K
Replies
3
Views
2K
Replies
10
Views
9K
Replies
14
Views
2K
Replies
8
Views
2K
Replies
4
Views
2K
Replies
2
Views
3K
Back
Top