|
|
 |
 |
 |
 |
Fortran Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
A question about "intent(out)"?
Bellow is a subroutine in my program. I use "integer(4),intent(out)::g(:)" but did not give it shape(Note: g(:) is not a global variable). However, the program can give right redults, why????? subroutine num_to_g(num, nf, g) implicit none integer(4),intent(in)::num(:), nf(:,:) integer(4),intent(out)::g(:) integer(4)::i, k, nod, nodof nod = ubound(num,1) nodof = ubound(nf,1) do i = 1, nod k = i*nodof g(k-nodof+1:k) = nf(:,num(i)) end do end subroutine num_to_g
On Jun 1, 5:41 am, li.sim@gmail.com wrote:
> Bellow is a subroutine in my program. I use > "integer(4),intent(out)::g(:)" but did not give it shape(Note: g(:) is > not a global variable). However, the program can give right redults, > why????? > subroutine num_to_g(num, nf, g) > implicit none > integer(4),intent(in)::num(:), nf(:,:) > integer(4),intent(out)::g(:) > integer(4)::i, k, nod, nodof > nod = ubound(num,1) > nodof = ubound(nf,1) > do i = 1, nod > k = i*nodof > g(k-nodof+1:k) = nf(:,num(i)) > end do > end subroutine num_to_g
Whenever you use assumed shape arguments (declared like x(:) or x(k:,:) etc.), then the shape of the dummy argument is inquired from the associated actual argument, i.e. the assumed shape arrays carry their dimensions with them.
The problem is that I didn't use "intent(in)" but "intent(out)". This means g(:) is no relation with the any input values. And g(:) can be only assgin a value in the subroutine itself, however,g(:) didn't has a shape from the first. So, if I assgin g(1)..g(10),then the output g(:) is a array contain 10 element; or if I assgin g(1)..g(100),then the output g(:) is a array contain 100 element.This,I'm puzzeled???
li.sim @gmail.com wrote: > The problem is that I didn't use "intent(in)" but "intent(out)". This > means g(:) is no relation with the any input values. And g(:) can be > only assgin a value in the subroutine itself, however,g(:) didn't has > a shape from the first. So, if I assgin g(1)..g(10),then the output > g(:) is a array contain 10 element; or if I assgin g(1)..g(100),then > the output g(:) is a array contain 100 element.This,I'm puzzeled??? The values come from the subroutine but the shape comes from the calling routine. If you use an ALLOCATABLE dummy argument then you can determine the shape in the called routine and ALLOCATE the array as needed. -- glen
| The problem is that I didn't use "intent(in)" but "intent(out)". This | means g(:) is no relation with the any input values. And g(:) can be | only assgin a value in the subroutine itself, however,g(:) didn't has | a shape from the first. So, if I assgin g(1)..g(10),then the output | g(:) is a array contain 10 element; or if I assgin g(1)..g(100),then | the output g(:) is a array contain 100 element.This,I'm puzzeled??? No, you get it wrong. For an assumed-shape array dummy, intent(OUT) specifies that the *contents* (values) of the array are to be filled by the routine, and not used within it before it defines them. However, array's *shape* (rank and dimensions) are still inherited from the actual argument. Note that INTENT never/(actually, rarely*) has any effect on the semantics of produced code; in other words, the generated machine code will be the same with or without it. It only imposes restrictions on what you may do within the code and improves readability. *) the exceptions I recall are allocatable array dummies and user-defined types with default initialization. In those cases, the argument is "cleared" (deallocated/default-initialized) on the entry to the routine if INTENT(OUT) is specified. -- Jugoslav ___________ www.xeffort.com Please reply to the newsgroup. You can find my real e-mail on my home page above.
On May 31, 9:41 pm, li.sim@gmail.com wrote: > subroutine num_to_g(num, nf, g) > implicit none > integer(4),intent(in)::num(:), nf(:,:) > integer(4),intent(out)::g(:) > integer(4)::i, k, nod, nodof > nod = ubound(num,1) > nodof = ubound(nf,1) > do i = 1, nod > k = i*nodof > g(k-nodof+1:k) = nf(:,num(i)) > end do > end subroutine num_to_g
FYI... the intrinsic function pack(array,mask) performs the same function as your subroutine.
|
 |
 |
 |
 |
|