Return Multiple variables in Fortran

AI Thread Summary
Fortran functions can only return a single variable, while subroutines can return multiple variables by passing them as arguments. Modifying a variable in a subroutine will also change it in the calling code, so using different placeholder names and the 'intent' attribute helps manage which variables are modified. Local variables within a subroutine do not get returned to the calling program, and any modifications to passed arguments can be controlled. A workaround for returning multiple values is to create separate functions for each variable, but this can complicate calculations that depend on the order of operations. The user ultimately decided to utilize subroutines with intent attributes for better control over variable modifications.
aditya.p
Messages
4
Reaction score
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
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.
 
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
 
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
 
Fortran arguments are passed by reference (pointers). In the early days of compilers, you could modify constants used as arguments in subroutines.
 
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.
 
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
 

Similar threads

Replies
3
Views
3K
Replies
4
Views
2K
Replies
2
Views
3K
Replies
5
Views
11K
Replies
7
Views
2K
Replies
8
Views
4K
Replies
3
Views
1K
Replies
5
Views
6K
Replies
8
Views
2K
Back
Top