Fortran external functions vs subroutines

  • Context: Fortran 
  • Thread starter Thread starter Vrbic
  • Start date Start date
  • Tags Tags
    Fortran Functions
Click For Summary

Discussion Overview

The discussion revolves around the organization and implementation of functions and subroutines in Fortran, particularly focusing on the use of external files for routines, the structure of code, and troubleshooting compiler errors. Participants explore best practices for coding in Fortran, referencing specific examples and common issues faced by beginners.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant questions the practice of collecting all functions and subroutines in an external file and whether it is advisable to mix functions and subroutines in the same file.
  • Another participant suggests that functions specific to a program should be kept with the program, while more general functions might be placed in a library for broader use.
  • Concerns are raised about the potential complications of modifying library functions that are used in multiple programs.
  • Several participants discuss the compiler errors encountered, with one suggesting that declaration statements must precede executable statements in Fortran.
  • Another participant points out a possible naming inconsistency in variable usage within the code, which could lead to errors.
  • There is a suggestion that beginners might benefit from keeping all code in one file initially to simplify the learning process.

Areas of Agreement / Disagreement

Participants express varying opinions on the organization of code and the use of libraries, with no consensus reached on the best approach. There is also disagreement regarding the interpretation of compiler errors and how to resolve them.

Contextual Notes

Some participants note that the code is based on Fortran 77, which may require adjustments depending on the compiler being used, as Fortran has evolved over time. There are also mentions of potential issues with variable declarations and naming conventions that remain unresolved.

Who May Find This Useful

This discussion may be useful for beginners in Fortran programming, particularly those interested in understanding the organization of code, troubleshooting common errors, and learning about the use of libraries in programming.

Vrbic
Messages
400
Reaction score
18
Hello, I'm fortran beginner. I have problem with calling or definition of functions and subroutines. I decided to establish my routine file, where I collect all functions and subroutines who I code (for now I have there function for derivative, only this works).
My first question, is it good way, to colect all in external file and call from it?
May I have functions and subroutins in same file?
May I call function from same routine file into subroutine in same file?
I'm following Numerical recipes in fortran 77 from Shapiro at all. Now I'm struggeling with runge-kutta 4 subroutine. I just write it into my routine file and don't call. To main program I call only derivative function above. But compiler reports many errors:
1)
INTEGER :: i
1
Error: Unexpected data declaration statement at (1)

2)
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
1
Error: Unexpected data declaration statement at (1)

3) atc.

atc. but I followed recipies...
Can you advice me?

My code
Main
Fortran:
DOUBLE PRECISION FUNCTION X2(a)
    DOUBLE PRECISION  :: a,OUT
    OUT=sin(a)
    RETURN
 END FUNCTION X2
DOUBLE PRECISION FUNCTION DDX2(a)
    DOUBLE PRECISION :: a,OUT
    OUT=cos(a)
    RETURN
 END FUNCTION DDX2
 
 PROGRAM HLAVNI
    IMPLICIT NONE
    INTEGER :: i
    DOUBLE PRECISION :: X, DX2, h
    DOUBLE PRECISION, EXTERNAL :: X2, DDX2
    h=10.0**(-5)

    WRITE(*,*) 'Zadejte ve kterem bode chcete derivovat'
    READ(*,*) X

    wRITE(*,*) 'Hodnota v bode x', X2(X)
    CALL derivs(X,X2,DX2)
    wRITE(*,*) 'Derivace v bode x', DX2
    wRITE(*,*) 'Opravdova derivace v bode x', DDX2(X)

    READ(*,*) X
 END PROGRAM
routines
Fortran:
!*********************************!
!****** Derivative function ******!
!*********************************!

DOUBLE PRECISION FUNCTION derivs(x,y,dydx)
  DOUBLE PRECISION :: x,y,dydx,h
  h=10.0**(-10)
  dydX=(y(x+h)-y(x-h))/(2*h)return
END FUNCTION derivs
!*********************************!
!***** Runge-Kutta 4th order *****!
!*********************************!

SUBROUTINE rk4(y,dydx,n,x,h,yout)
INTEGER :: n,NMAX
DOUBLE PRECISION :: h,x,dydx(n),y(n),yout(n)
!DOUBLE PRECISION, EXTERNAL :: derivs
NMAX=50                          !Maximum number of equation

INTEGER :: i
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
hh=h*0.5
h6=h/6.
xh=x+hh
                  !I move calls to cycle and add (i) to dyt(i) atc.
                               !First step
do i=1,n                     
   yt(i)=y(i)+hh*dydx(i)
   call derivs(xh,yt,dyt(i))
end do

                               !Second step
do i=1,n                     
   yt(i)=y(i)+hh*dyt(i)
   call derivs(xh,yt,dym(i))
end do

                                !Third step
do i=1,n
   yt(i)=y(i)+hh*dym(i)
   dyn(i)=dyt(i)+dym(i)
   call derivs(x+h,yt,dyt(i))
end do

                                !Fourth step
do i=1,n
   yout(i)=y(i)+h6*(dydx(i)+dyt(i)+2.*dym(i))
end do
return
END SUBROUTINE
 
Technology news on Phys.org
In general, programmers keep functions specific to a program with the program and functions have more general usage in a library of functions.

As an example, if you wrote functions for computing trig values then you’d place them in a library so they could used in other programs that might be written.

In contrast, if you write a function for a specific task needed only by this program then place it with the program.

Sometimes program specific functions are needed in other programs and so then it makes sense to promote those functions to a library for others to use.

Be aware that placing functions in a library comes at a cost ie you can’t just change them without testing them in other programs that rely on them.
 
  • Like
Likes   Reactions: FactChecker
jedishrfu said:
In general, programmers keep functions specific to a program with the program and functions have more general usage in a library of functions.

As an example, if you wrote functions for computing trig values then you’d place them in a library so they could used in other programs that might be written.

In contrast, if you write a function for a specific task needed only by this program then place it with the program.

Sometimes program specific functions are needed in other programs and so then it makes sense to promote those functions to a library for others to use.

Be aware that placing functions in a library comes at a cost ie you can’t just change them without testing them in other programs that rely on them.
Ok, I understand. So how may I create such library? Or what is different from my file of routines?
I mean, derivative functions is quite general also subroutine for solution of ODEs. I guess library should has different suffix like *.lib? And how should I call some function or subroutine from it?
Can you please check out errors? What is wrong? I feel that something very general but I don't have idea what.

Thank you.
 
You said your code is from Fortran 77 but what compiler are you using? Fortran has evolved over the years and you’ll need to find a howto document for upgrading the code to the newer syntax if that’s the case.
 
jedishrfu said:
You said your code is from Fortran 77 but what compiler are you using? Fortran has evolved over the years and you’ll need to find a howto document for upgrading the code to the newer syntax if that’s the case.
I believe it is not problem. I rewrite it. Maybe there can be some small buck but I can't see it. As I said. Calling function for derivative working. I just wrote subroutine bellow function I don't call it and it finds errors. In declaration, I have same declarations above and everything is alright...
May be a problem that I declared variables two times?
 
I saw an assignment where you used dydX and not dydx as you defined in your earlier statements.

That’s derivs function.

Also shouldn’t your return have a variable to return like return dydx ?
 
  • Like
Likes   Reactions: Vrbic
Vrbic said:
I'm following Numerical recipes in fortran 77 from Shapiro at all. Now I'm struggeling with runge-kutta 4 subroutine. I just write it into my routine file and don't call. To main program I call only derivative function above. But compiler reports many errors:
1)
INTEGER :: i
1
Error: Unexpected data declaration statement at (1)

2)
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
1
Error: Unexpected data declaration statement at (1)

Vrbic said:
Fortran:
!*********************************!
!***** Runge-Kutta 4th order *****!
!*********************************!

SUBROUTINE rk4(y,dydx,n,x,h,yout)
INTEGER :: n,NMAX
DOUBLE PRECISION :: h,x,dydx(n),y(n),yout(n)
!DOUBLE PRECISION, EXTERNAL :: derivs
NMAX=50                          !Maximum number of equation

INTEGER :: i
DOUBLE PRECISION :: h6,hh,xh,dym(NMAX),dyt(NMAX),yt(NMAX)
<snip>
@Vrbic, I believe that the two compiler errors you are seeing are due to your RK4 subroutine, quoted above.
You have an executable statement, NMAX = 50, followed by two declaration statements. If I'm remembering correctly, all declaration statements have to come before any executable statements. You should probably be able to fix the two compiler errors you listed by moving the NMAX assignment statement below the declaration for h, x, dydx(n), etc.
 
  • Like
Likes   Reactions: jedishrfu
Mark44 said:
@Vrbic, I believe that the two compiler errors you are seeing are due to your RK4 subroutine, quoted above.
You have an executable statement, NMAX = 50, followed by two declaration statements. If I'm remembering correctly, all declaration statements have to come before any executable statements. You should probably be able to fix the two compiler errors you listed by moving the NMAX assignment statement below the declaration for h, x, dydx(n), etc.
Fixed, thank you.
 
As a beginner developing new code, you may want to put everything in one file just to keep things as simple as possible and only worry about the FORTRAN syntax. But as soon as you get a little more experience, you should learn how to use libraries as described by @jedishrfu .
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
10K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K