I've only been learning Fortran 77 (and its syntax) the last few days, and tried finding answers throughout the site and textbooks already and come up still confused, so I'd appreciate any help. I'm sorry if the formatting on this post is off; this is my first post, and I'm crunched for time.
I'm creating a program to multiply matrices. I want to create a subroutine or a function that will take two matrices as inputs (two 2x2 arrays), and return the multiplied matrix (one 2x2 array). I can't figure out how to get either a subroutine or a function to return an array of fixed size, or how to use the array once it's returned.
I tried using a function, which compiled on its own. But when I tried calling the function from my main program, I couldn't call it on its own:
MATMULT(MAT0, MAT0, MAT0) 1 Error: Unclassifiable statement at (1)
or assign it to another variable (I tried different REALs and arrays):
BLAH = MATMULT(MAT0, MAT0, MAT0) 1 Error: Return type mismatch of function 'matmult' at (1) (INTEGER(4)/REAL(4)) MATRIX1.f:26.22: BLAH = MATMULT(MAT0, MAT0, MAT0) 1 Warning: Type mismatch in argument 'x' at (1); passed INTEGER(4) to REAL(4) BLAH = MATMULT(MAT0, MAT0, MAT0) 1 Warning: Rank mismatch in argument 'x' at (1) (scalar and rank-2)
Since arrays are passed by reference, I'm really not sure what the function is returning, so how can I use the output matrix, if that is indeed the function's output?
I also tried using a subroutine, but (in addition to still not knowing what it's returning or where) then I get a "Two main PROGRAMs" error - so the compiler isn't differentiating between the main program and the subroutine. This might be a problem with my syntax on subroutines? I tried a few different things, but here's my most recent iteration of the code (I'm just trying to get the array-passing to work, so there's not actual matrix multiplication in here yet):
PROGRAM MATRIX1 INTEGER N REAL A, B, MAT0(2,2), MATF(2,2), X(2,2), Y(2,2), Z(2,2) REAL BLAH PRINT *, " ENTER THE VALUE OF A: " READ *, A PRINT *, " ENTER THE VALUE OF B: " READ *, B PRINT *, " ENTER THE NUMBER OF MULTIPLICATIONS: " READ *, N
C Creates the initial matrix
MAT0(1,1) = 1.0 - A MAT0(1,2) = A MAT0(2,1) = B MAT0(2,2) = 1.0 - BPRINT *, "M = ", MAT0 CALL MATMULT(MAT0, MAT0, MAT0) PRINT *, "FINAL " STOP END PROGRAM REAL SUBBROUTINE MATMULT(X, Y, Z) END SUBROUTINE
Or (edited to add some of the recommended changes) with a function:
PROGRAM MATRIX1 INTEGER N REAL A, B, MAT0(2,2), MATF(2,2), X(2,2), Y(2,2), Z(2,2) REAL MATMULT(2,2) PRINT *, " ENTER THE VALUE OF A: " READ *, A PRINT *, " ENTER THE VALUE OF B: " READ *, B PRINT *, " ENTER THE NUMBER OF MULTIPLICATIONS: " READ *, N
C Creates the initial matrix
MAT0(1,1) = 1.0 - A MAT0(1,2) = A MAT0(2,1) = B MAT0(2,2) = 1.0 - BPRINT *, "M = ", MAT0 Z = MATMULT(X, Y) STOP END PROGRAM FUNCTION MATMULT(X, Y) REAL X(2,2), Y(2,2), Z(2,2), MATMULT(2,2) RETURN END
I'm still getting errors:
Z = MATMULT(X, Y) 1 Warning: Legacy Extension: REAL array index at (1) MATRIX1.f:28.19: Z = MATMULT(X, Y) 1 Error: Array index at (1) is an array of rank 2