Return Multiple variables in Fortran

In summary, a fortran subroutine can return multiple variables, but they will be passed by reference. You can use intent to make sure that only the variables you want to edit are edited. Alternatively, you can write many functions which each return only one variable and use them in a stage-wise manner to calculate the various multiples which were returned by a single function. Thanks.
  • #1
aditya.p
5
0
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.

Thanks

Aditya
 
Physics news on Phys.org
  • #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.
 
  • #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?

Thanks

Aditya
 
  • #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?

Thanks

Aditya
 
  • #5
Fortran arguments are passed by reference (pointers). In the early days of compilers, you could modify constants used as arguments in subroutines.
 
  • #6
aditya.p said:
Does a fortran subroutine online return the arguments which are passed to it or does it return all variables inside the subroutine.
No. The variables local to the subroutine will not be returned to the calling program.

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?
. 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.
 
  • #7
Hey,

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.

Thanks

Aditya
 

Related to Return Multiple variables in Fortran

1. How can I return multiple variables in Fortran?

Fortran allows you to use the "return" statement to return multiple variables from a function. You can specify the variables to be returned after the "return" keyword, separated by commas. For example: return var1, var2, var3.

2. Can I use arrays to return multiple variables in Fortran?

Yes, you can use arrays to return multiple variables in Fortran. You can declare an array to store the variables you want to return, and then use the "return" statement to return the entire array.

3. How do I access the returned variables in my main program?

To access the returned variables in your main program, you can assign them to variables with the same data type. For example, if your function returns an integer and a real number, you can assign them to integer and real variables respectively.

4. Is there a limit to the number of variables that can be returned in Fortran?

There is no specific limit to the number of variables that can be returned in Fortran. However, it is good practice to keep the number of returned variables to a minimum for better code readability and maintainability.

5. Can a subroutine also return multiple variables in Fortran?

No, a subroutine cannot return multiple variables in Fortran. Subroutines are used for procedures that do not return a value, while functions are used to return a single value or multiple values. If you want to return multiple variables, you should use a function instead of a subroutine.

Similar threads

  • Computing and Technology
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
819
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
640
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
26
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top