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

Return Multiple variables in Fortran

  1. Apr 28, 2010 #1
    Hey Guys,

    I am converting my code from matlab to fortran. Now in matlab it is rather convenient to write a function which returns multiple variables.

    I believe a function in fortran can return only one variable. I think a subroutine can return multiple variables. I was not able to find any syntax help for fortran to write such a routine. I was curious if anyone has any insight into this.


  2. jcsd
  3. Apr 28, 2010 #2
    Modifying a variable in a subroutine or function in fortran will also modify it in the calling code. So just pass the multiple variables as arguments and then assign the desired values to them at the end.
  4. Apr 28, 2010 #3
    Thanks. That did occur to me. but another freedom which matlab affords is internal variables. So I use a bunch of variables within a matlab function which do not get returned. Does a fortran subroutine online return the arguments which are passed to it or does it return all variables inside the subroutine.

    In the event it returns everything, is there a way to selectively clear variables like in matlab?


  5. Apr 28, 2010 #4
    Also, there are case in my matlab program where I pass a variable as an argument and the argument itself gets modified and comes out as a modified argument.

    So the argument and the variable returned are the same.
    Can something like this be performed in fortran?


  6. Apr 28, 2010 #5


    User Avatar
    Homework Helper

    Fortran arguments are passed by reference (pointers). In the early days of compilers, you could modify constants used as arguments in subroutines.
  7. Apr 28, 2010 #6
    No. The variables local to the subroutine will not be returned to the calling program.

    . I'm not quite sure what you mean. Inside a fortran subroutine, you can always copy the value of an argument to a local variable if you don't want to modify the argument.
  8. Apr 28, 2010 #7

    Thanks for the response. I figured out how to use the subroutine to return multiple variables and at the same time make sure that any other variables outside are not affected.

    First I made sure that I used different name place-holders within the subroutine.

    Second I used intent to make sure that only the variables I want to be edited are edited.

    So now I pass in the variables which are to be calculated into the subroutine along with the other arguments and depending on whether I am acting on the values input and changing them or calculating the output all together I specify the intent as either in, out out inout.

    Another work around would be write many functions which each return only one variable and keep using them in a stage-wise manner to calculate the various multiples which were returned by a single function, except that I will write a function for each variable.

    This becomes slightly tedious in the event one variable which is calculated is used to calculate the other variable in the subsequent function. So I have to pay close attention to the order. This method is more secure than the subroutine one as I need not worry about by mistake using a common variable name which may effect some variable in the main program. But it is also more tedious. So I ended up using the subroutines and securing the arguments using intent.


Share this great discussion with others via Reddit, Google+, Twitter, or Facebook