tesaoghost
Programmer
- Jan 9, 2014
- 11
Hi everybody
I have a little problem with functions and subroutines in fortran! I want to write a program that takes one or more array in main program, then sends this arrays to a function or subroutine and return an array to main function again. For example consider a program that takes (a) and (b) arrays in main program, sends them to a function which returns multiple of a and b matrices as a 2D array. I wrote this program using internal function:
program Matrix_Operation
implicit none
real :: a(20,20),b(20,20),mult(20,20)
integer :: i,j,n
n = 2
read *,((a(i,j),i=1,n),j=1,n)
read *,((b(i,j),i=1,n),j=1,n)
mult = matrix_multiple(a,b,n)
print *,"A * B ="
Do i=1,n
print "(100(F10.4,TR3))",(mult(i,j),j=1,n)
End Do
!*****************************************
! Multiplier Function Definition
!*****************************************
contains
function matrix_multiple(x,y,n)
implicit none
real,intent(in) :: x(20,20),y(20,20)
integer,intent(in) :: n
real :: m(20,20),matrix_multiple(20,20)
integer :: i,j,k
Do i=1,n
Do j=1,n
Do k=1,n
m(i,j) = m(i,j)+x(i,k)*y(k,j)
End Do
End Do
End Do
matrix_multiple = m
End function matrix_multiple
!*****************************************
! End of Function Definition
!*****************************************
End program Matrix_Operation
but I want to know is there any way to write this program using external functions i.e. a function that its determination is after main program?
I have a little problem with functions and subroutines in fortran! I want to write a program that takes one or more array in main program, then sends this arrays to a function or subroutine and return an array to main function again. For example consider a program that takes (a) and (b) arrays in main program, sends them to a function which returns multiple of a and b matrices as a 2D array. I wrote this program using internal function:
program Matrix_Operation
implicit none
real :: a(20,20),b(20,20),mult(20,20)
integer :: i,j,n
n = 2
read *,((a(i,j),i=1,n),j=1,n)
read *,((b(i,j),i=1,n),j=1,n)
mult = matrix_multiple(a,b,n)
print *,"A * B ="
Do i=1,n
print "(100(F10.4,TR3))",(mult(i,j),j=1,n)
End Do
!*****************************************
! Multiplier Function Definition
!*****************************************
contains
function matrix_multiple(x,y,n)
implicit none
real,intent(in) :: x(20,20),y(20,20)
integer,intent(in) :: n
real :: m(20,20),matrix_multiple(20,20)
integer :: i,j,k
Do i=1,n
Do j=1,n
Do k=1,n
m(i,j) = m(i,j)+x(i,k)*y(k,j)
End Do
End Do
End Do
matrix_multiple = m
End function matrix_multiple
!*****************************************
! End of Function Definition
!*****************************************
End program Matrix_Operation
but I want to know is there any way to write this program using external functions i.e. a function that its determination is after main program?