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

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...
 Share

×
×
  • Create New...