Return Multiple variables in Fortran

Click For Summary

Discussion Overview

The discussion centers on the challenges of returning multiple variables in Fortran, particularly in comparison to MATLAB. Participants explore the use of subroutines and functions in Fortran to achieve similar functionality, addressing issues related to variable modification and scope.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • Aditya notes that MATLAB allows functions to return multiple variables, while he believes Fortran functions can only return one variable and subroutines can return multiple variables.
  • One participant suggests that modifying a variable in a Fortran subroutine will also modify it in the calling code, recommending passing multiple variables as arguments.
  • Aditya expresses concern about internal variables in MATLAB functions that do not get returned and questions whether Fortran subroutines return only passed arguments or all internal variables.
  • Another participant clarifies that local variables in a Fortran subroutine will not be returned to the calling program and discusses the ability to copy argument values to local variables to avoid modification.
  • Aditya describes his solution involving the use of different name placeholders and the `intent` attribute to control which variables are modified in the subroutine.
  • He also considers the alternative of writing multiple functions that return single variables, noting the complexity of managing dependencies between these functions.

Areas of Agreement / Disagreement

Participants generally agree on the use of subroutines to return multiple variables in Fortran, but there is some uncertainty regarding the handling of internal variables and the implications of variable modification. The discussion remains unresolved on the best practices for managing variable scope and modification.

Contextual Notes

Limitations include the potential for confusion regarding variable scope and the behavior of arguments in Fortran subroutines versus MATLAB functions. The discussion does not resolve the nuances of these differences.

Who May Find This Useful

This discussion may be useful for programmers transitioning from MATLAB to Fortran, particularly those interested in understanding variable handling and subroutine functionality in Fortran.

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 ·
Replies
3
Views
3K
Replies
3
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
11K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 26 ·
Replies
26
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K