0

Well, I have this basic function:

  real(8) function db(ER)
    implicit none
    real(8) :: ER  
    real(8) :: A=-0.14600d0
    real(8) :: B=1.093898d0    
    real(8) :: C

    C=B/(2.0d0*A)    
    print*, ER, ER/A, C**2
    db=-sqrt((ER/A) + C**2) - C    
    return
  end function db

And this function is called from other file (STP)

  program STP
      implicit none
      real(8) :: db

      print*, 'Waiting ... '
      print*, db(1.0d0)
      return
  end program STP

Finally in my terminar:

  ususario@us:  gfortran STP.f90 db.f90
  ususario@us:  ./a.out
  Waiting ...

But the problem is coming: my terminal never print the values in the print statement inside of the function 'db'. The behavior persists even when I change 'print*, ER, ER/A, C**2'
to print*, 'hola'.

I tried to put the code in a single file, like this

  program prueba
    implicit none
    real(8) :: db

    print*, db(0.0d0)
  end program prueba

  real(8) function db(ER)
    implicit none
    real(8) :: ER  
    real(8) :: A=-0.14600d0
    real(8) :: B=1.093898d0    
    real(8) :: C

    C=B/(2.0d0*A)    
    print*, ER, ER/A, C**2
    db=-sqrt((ER/A) + C**2) - C
    return
  end function db

and the values ER, ER/A, C**2 are printed in the terminal

Jogabell
  • 19
  • 6
  • 1
    How do you reference the function and what is the exact error message? Please show a complete example ([mre]). In particular, if you have something like `print*, db(1._8)` then that certainly won't work with the print statement in the function – francescalus Jun 04 '20 at 22:24
  • 2
    Recursive output is addressed in this [other question](https://stackoverflow.com/q/24923076/3157076). – francescalus Jun 04 '20 at 22:34
  • 1
    If it is not recursive I/O can you please explain what you mean by "don't work" - what error message do you get? Or does it give the wrong answer? – Ian Bush Jun 05 '20 at 08:42
  • 1
    Also please learn while real(8) is bad practice - https://stackoverflow.com/questions/838310/fortran-90-kind-parameter – Ian Bush Jun 05 '20 at 08:44
  • 1
    This is an example of recursive input/output. It doesn't matter what you try to print in the function: you aren't allowed to print anything in the function when there's an active output statement to the same unit which invokes the function. Full explanation and alternative approaches are given in answer to the other question. – francescalus Jun 06 '20 at 22:25

0 Answers0