Jump to content

function and subroutine in Fortran 77


Recommended Posts

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

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

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 by Stephni
Link to comment
Share on other sites

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...