Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Transmitting values to functions and subroutines problems - Fortran 90

  1. Oct 20, 2013 #1
    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
     
  2. jcsd
  3. Oct 20, 2013 #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 (Text):

    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.
     
  4. Oct 21, 2013 #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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Transmitting values to functions and subroutines problems - Fortran 90
Loading...