Reputation:
How to globalize a dummy argument from a function call?
I have the below code in Fortran
A(a,b) // here a and b are the values filling from function call but they are not declared any where
.
.
.
B(a.b) // same variable passing again to this function call also.
here the problem is the values from a and b are not maintaining for second call. it's returning a garbage. Even I tried this using common but its not accepting to globalize dummy arguments. How to do it?
Upvotes: 0
Views: 342
Reputation: 1555
Use the SAVE
declaration to store the state across invocations of the subroutine or function.
Upvotes: 1
Reputation: 396
Dummy variables are by nature, that: dummy arguments. They are simply placeholders, and when a function is called, they get replaced with whatever you passed the function when calling it. lets look at a silly example:
integer function add(x,y)
integer :: x, y
add = x + y
end
so now, you can call this function in many ways. lets say you want to add a and b:
integer :: a, b, c
a = 3
b = 4
c = add(a,b)
here, a will take the position of x in the function, and b will take the position of y. c will have the value of 7. you could have just as easily done:
integer :: c
c = add(3,4)
with the same results. as you can see, globalizing dummy arguments is meaningless.
Upvotes: 0
Reputation: 10677
You say that "a
and b
are the values filling from function call but they are not declared anywhere" - even though I'm still trying to figure out exactly what you're trying to do, this statement throws up all sorts of red flags. This is Fortran - you need to declare them (see note).
I'm not clear on what you're trying to accomplish - chances are, though, "globalize" isn't what you want to do - we should be striving to eliminate global variables in our code.
Note: Yes, Fortran (or really, FORTRAN) supports implicit declaration of variables. However, this is a relic from the days of punch cards and dumb terminals. Do Not use implicit typing anywhere - your code should always include implicit none
, and you should declare your variables appropriately.
Upvotes: 1