- #1
Biederman
- 9
- 0
Hi guys,
I am kind of a newbie to fortran and I'd like to ask a simple question.
In the code below, I have a main program with one subroutine. It's a simple one.
In the main code I am filling an array with elements from 1 to 10. Then I am passing this array to a subroutine "sub", where I am computing the logarithms of the array elements. However, I don't want to return the array back into the main program. In fact, I want the the logarithms to be calculated in the subroutine only.
However, when I print the array elements just after the call to the subroutine, I notice that the array elements are actually the logarithms of the latter. When I print the elements before to the call of the subroutine, the elements print fine i.e 1,2,3,4...etc. How do say fortran not to alter the array elements in the main program. I am asking this because later, I need to pass the original array elements to a second subroutine. That is, if a call a second subroutine e.g "sub2" with the same arguments as the first one, I notice that the main program actually passes the logarithms i.e the elements which have been calculated in the first subroutine.
I hope my question is clear enough.
BTW: I prefer to use fortran 77. So, if you can help me by providing a fortran 77 solution, I'd appreciate that!
c***********************************************************************
implicit none
integer i,n
parameter(n=10)
real arr(n)
c***********************************************************************
do i = 1, 10
arr(i) = real(i)
end do
print*,arr ! here it prints: 1,2,3,4 ...etc which is what I want
call sub(n,arr)
print*,arr ! here it prints the logarithm of the values, which is not what I want. I'd like the origininal array because I am going to pass it to another subroutine.
end
c***********************************************************************
subroutine sub(n,ar)
integer i,n
real :: ar(n)
do i = 1, 10
ar(i) = alog10(ar(i))
end do
end
I am kind of a newbie to fortran and I'd like to ask a simple question.
In the code below, I have a main program with one subroutine. It's a simple one.
In the main code I am filling an array with elements from 1 to 10. Then I am passing this array to a subroutine "sub", where I am computing the logarithms of the array elements. However, I don't want to return the array back into the main program. In fact, I want the the logarithms to be calculated in the subroutine only.
However, when I print the array elements just after the call to the subroutine, I notice that the array elements are actually the logarithms of the latter. When I print the elements before to the call of the subroutine, the elements print fine i.e 1,2,3,4...etc. How do say fortran not to alter the array elements in the main program. I am asking this because later, I need to pass the original array elements to a second subroutine. That is, if a call a second subroutine e.g "sub2" with the same arguments as the first one, I notice that the main program actually passes the logarithms i.e the elements which have been calculated in the first subroutine.
I hope my question is clear enough.
BTW: I prefer to use fortran 77. So, if you can help me by providing a fortran 77 solution, I'd appreciate that!
c***********************************************************************
implicit none
integer i,n
parameter(n=10)
real arr(n)
c***********************************************************************
do i = 1, 10
arr(i) = real(i)
end do
print*,arr ! here it prints: 1,2,3,4 ...etc which is what I want
call sub(n,arr)
print*,arr ! here it prints the logarithm of the values, which is not what I want. I'd like the origininal array because I am going to pass it to another subroutine.
end
c***********************************************************************
subroutine sub(n,ar)
integer i,n
real :: ar(n)
do i = 1, 10
ar(i) = alog10(ar(i))
end do
end