0
votes

Seems I am not good at either FORTRAN or MATLAB while knowing a little bit about both of them.

While I found MATLAB is good at handling matrices, I also found implicit interface in a fortran program is rather convenient (I can pass claimed variables, especially some large arrays, to implicit functions without put them as input dummy arguments, and also I can pass variables claimed in the interface back to the calling program easily).

I am just wondering whether there is any similar mechanism in MATLAB as implicit interface so that I can do the work as I do with FORTRAN. (Global variable seems not very good since, if I try to call the function often, it would just turned to a tedious work -- maybe I am wrong)

What's your opinion? Thanks.


Here is an example:

PROGRAM test_function
IMPLICIT NONE
REAL :: A, B
REAL :: C,D,E

A = 1
B = 2
D = 3
E = xf(A)-A

WRITE (*,*), "A = ", A
WRITE (*,*), "B = ", B
WRITE (*,*), "C = ", C
WRITE (*,*), "D = ", D  
WRITE (*,*), "E = ", E  

CONTAINS
FUNCTION xf(x)
    IMPLICIT NONE
    REAL, INTENT(IN) :: x
    REAL :: xf
    C = x+B
    D = x+D
    xf = A+B+C
END FUNCTION xf

END PROGRAM test_function

D is passed into the Function xf(·) without being taken as a dummy argument, and D could also be passed out with no restrictions. The result given by the program is as follows:

 A =    1.0000000    
 B =    2.0000000    
 C =    3.0000000    
 D =    4.0000000    
 E =    5.0000000
1
It's not clear to me what an implicit interface in Fortran is. From the text of your question, does that mean that variables (perhaps with the same name) are somehow passed in and out of subroutines without being passed as arguments?Pursuit
It is very clear to me what an implicit interface in Fortran is. But the usage of the term in the question makes me suspect that OP uses the term in a non-(Fortran)-standard way. Could OP be more explicit ?High Performance Mark
@Lagrange: could you re-edit your code snippets and delete them from the comments. It's very difficult to figure out what your program is right now.High Performance Mark
Sorry for the ambiguous description. I edited my post with an example. Hope this can make my question easy to understand. ThxL.J
It seems to me that he's saying that the compiler generates a explicit interface without the user needing to. Additionally, the function xf inherits the scope of the parent block (in this case, the main program). of course, in fortran, xf can only be called from the parent block, no other program units can access that routine.mgilson

1 Answers

0
votes

I think I begin to understand your question now.

For the record, this feature of Fortran, that you can apparently pass arguments to functions without referring to them in the function signature, is a BAD THING. And of course, it only works here because the function definition is contained in the same source scope as the program itself.

If you are over 18 go right ahead and do dangerous things in your programs. If you are not, stop immediately and do things properly -- do not pass arguments into functions other than in their argument lists, and do not pass anything out of a function other than through the result of the function itself.

You can sort of do this in Matlab too. If you put a variable called, say, 'a' in your workspace, then any script which refers to a variable called 'a' refers to that variable. It's different for Matlab functions, which define scoping units of their own. To refer to a workspace variable from within a Matlab function you either have to pass it in or use the global variable capabilities. If you don't understand the difference between scripts and functions in Matlab, hit the documentation.