Stephni Posted November 18, 2009 Share Posted November 18, 2009 Hi everyone I would like to know the difference between the function and subroutine in FORTRAN 77, and what kind of jobs each one is convenient for?? because I need to write a routine to do some job, but I do not know whether it should be a function or a subroutine??? thanks for help Stefani Link to comment Share on other sites More sharing options...
daniewicz Posted November 19, 2009 Share Posted November 19, 2009 The best way to show you the difference between a function and a subroutine is to show you a simple example. The program demo will generate a result answ1 and answ2 through a subroutine call and a function call respectively. Both answ1 and answ2 equal 5. program demo call subroutine sum(2,3,answ1) write(*,*)answ1 answ2=sum(2,3) write(*,*)answ2 stop end subroutine sum(x,y,z) z=x+y return end function sum(x,y) sum=x+y return end Link to comment Share on other sites More sharing options...
Stephni Posted November 19, 2009 Author Share Posted November 19, 2009 (edited) thanks a lot, I think function is suitable for my job, but I've got a problem when I build a function, I need my function to produce a matrix, then I called the function in the main program by its name to give the matrix to the main program, but it does not work at all, I mean the matrix is computed correctly in the function but I failed to call it to the main program, here what I have in my code main_program c complex*16 A(m1,m2), Mat EXTERNAL Mat c A = MainMat(n,m) . .. end complex*16 function Mat(n,m) c calculate the elemnts of Mat Mat = A . end I need to return the matrix Mat, which I computed by the function to the main program Edited November 19, 2009 by Stephni Link to comment Share on other sites More sharing options...
daniewicz Posted November 20, 2009 Share Posted November 20, 2009 I don't think the EXTERNAL statement is necessary. I see the matrix is dimensioned in the main program. Is it dimensioned in the function too? Link to comment Share on other sites More sharing options...
Stephni Posted November 20, 2009 Author Share Posted November 20, 2009 Thanks for help,..... I have the following I took off the external statement, and declare the function (Mat) in my main program as complex*16 A, Mat C then I call it as A = MainMat(S1,S2) But when I've done the following loop, it does not work do i = 1,N do j = 1,M write(7,*) A(i,j) end do end do so I changed in the declaration as A(N,M), BUT IT DOES NOT WORK AS WELL Link to comment Share on other sites More sharing options...
daniewicz Posted November 21, 2009 Share Posted November 21, 2009 This tells me A is a complex number complex*16 A, Mat This tells me A is a matrix A = MainMat(S1,S2) Can't be both! Link to comment Share on other sites More sharing options...
Stephni Posted November 22, 2009 Author Share Posted November 22, 2009 yes the matrix A with complex enrties, so the value returned by a matrix should be a complex, is there anything not logical?? and why Can't be both! Link to comment Share on other sites More sharing options...
daniewicz Posted November 22, 2009 Share Posted November 22, 2009 OK. I went through some of my fortran files. Here is an example of a function which uses complex numbers. Does this help? COMPLEX FUNCTION OMEGA(ZETA) COMMON/DATA/A(4),B(4) COMPLEX ZETA,S S=(0.0,0.0) DO 10 J=1,4 S=S-A(J)/(B(J)+ZETA) 10 CONTINUE OMEGA=ZETA+S RETURN END Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now