Jump to content
Sign in to follow this  
Stephni

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

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...