INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Transfer of data from inside a subroutine to main program

Transfer of data from inside a subroutine to main program

Transfer of data from inside a subroutine to main program

(OP)
Hello,

The following program does not compile but returns the following error:

error #6236: A specification statement cannot appear in the executable section.

All I would like to do is to be able to transfer the array r(i) from the subroutine to the main program without using the "contents"
statement. The actual program I am working on is much bigger but the issue is the same, i.e., getting the main program to be able to
use r(i) inside the subroutine.

Program Test
IMPLICIT NONE
real,dimension(1) :: r

CALL f(r)

write(*,*) r

subroutine f(r)
implicit none
integer :: i
real, intent(out) :: r(i)

do i = 1, 10
r = 2*i
enddo
end subroutine
END

Thank you,

Vahid

RE: Transfer of data from inside a subroutine to main program

just curious, why not just use a common block then any information put into the common block in the subroutine is available in the main body of the procedure.

Bill
Lead Application Developer
New York State, USA

RE: Transfer of data from inside a subroutine to main program

Hi Vahid9,
IMHO, with contains it's more elegant, for example you could do it like this:
vahid9_01.f95

CODE

program test
  implicit none
  real, dimension(1) :: r1
  real, dimension(3) :: r2
  call f(r1)
  write(*,*) "r1= ", r1
  call f(r2)
  write(*,*) "r2= ", r2

contains
  subroutine f(r)
    implicit none
    integer :: i, n
    real, dimension(:), intent(out) :: r
    n=size(r)
    do i = 1, n
      r(i) = 2*i
    enddo
  end subroutine
end program 

when you want to do it without contains, so for example like this:
vahid9_02.f95

CODE

program test
  implicit none
  real, dimension(1) :: r1
  real, dimension(3) :: r2
  call f(r1, 1)
  write(*,*) "r1= ", r1
  call f(r2, 3)
  write(*,*) "r2= ", r2
end program

subroutine f(r, n)
  implicit none
  integer :: i, n
  real, dimension(n), intent(out) :: r
  do i = 1, n
    r(i) = 2*i
  enddo
end subroutine 


Output of both examples is the same:

CODE

mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ ./vahid9_02
 r1=    2.00000000    
 r2=    2.00000000       4.00000000       6.00000000 

RE: Transfer of data from inside a subroutine to main program

(OP)
Hi Bill and Mikrom,

Thank you for your responses. I have not used common blocks in the past and so I will look into it.

I tried using "contains" before. The actual code has several other subroutines and using "contains" results in a lot of other errors which
without the subroutine f(r), those errors do not occur.

The vahid9_02.f95 is almost what I am looking for except in my case, there is no input n to the subroutine. The subroutine calculates an array r(i) internally and all I would like is to be able to use that array outside the subroutine.

I am grateful for your suggestions.

Best wishes,
Vahid

RE: Transfer of data from inside a subroutine to main program

Hi Vahid9,

In the case without contains I declared the second argument (i.e. n = dimension of the array) in the funcion f(r, n) instead of only f(r), because my compiler (gfortran version 4.8.4) complained that:

CODE

call f(r1)
        1
Error: Procedure 'f' at (1) with assumed-shape dummy argument 'r' must have an explicit interface 

So when you don't want to have f(r, n), but only f(r) you must provide this explicit interface for the subroutine f.
This works for me - I modified vahid9_01.f95, so that I omitted contains and declared interface

vahid9_011.f95

CODE

program test
  implicit none
  real, dimension(1) :: r1
  real, dimension(3) :: r2
  interface
    subroutine f(r)
      implicit none
      integer :: i, n
      real, dimension(:), intent(out) :: r
    end subroutine
  end interface

  call f(r1)
  write(*,*) "r1= ", r1
  call f(r2)
  write(*,*) "r2= ", r2
end program

subroutine f(r)
  implicit none
  integer :: i, n
  real, dimension(:), intent(out) :: r
  n=size(r)
  do i = 1, n
    r(i) = 2*i
  enddo
end subroutine 

Output

CODE

mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ gfortran vahid9_011.f95 -o vahid9_011mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ ./vahid9_011
 r1=    2.00000000    
 r2=    2.00000000       4.00000000       6.00000000 

RE: Transfer of data from inside a subroutine to main program

(OP)
Thank you Mikrom. The interface made it work.

Best wishes,
Vahid

RE: Transfer of data from inside a subroutine to main program

you are welcome

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close