Simple Fortran Program Help: Calling Precision Function | Program Alpha

  • Context: Fortran 
  • Thread starter Thread starter Thibaut
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The discussion centers on a Fortran program named "Alpha" that fails to call a precision function correctly. The issue arises because the precision function is not declared with the correct type in the main program, leading the compiler to assume it returns a real*4 variable. To resolve this, the user must declare the precision function as real*8 in the main program. Without this declaration, the program may crash or produce incorrect results.

PREREQUISITES
  • Understanding of Fortran programming language syntax
  • Knowledge of data types in Fortran, specifically real*8 and real*4
  • Familiarity with function declarations and calls in Fortran
  • Basic debugging techniques in Fortran
NEXT STEPS
  • Learn about Fortran function declarations and their return types
  • Explore debugging techniques for Fortran programs
  • Study data type conversions in Fortran to avoid type-related errors
  • Investigate best practices for writing and organizing Fortran code
USEFUL FOR

This discussion is beneficial for Fortran programmers, software developers working with scientific computing, and anyone troubleshooting function call issues in Fortran applications.

Thibaut
Messages
1
Reaction score
0
Hello,
I'm writing a really simple program in fortran which is a part of a bigger program.
It is not working at the moment, but I really don't know why.

I am calling a function called precision, but the program does not enter it ( I know it since I use write statements inside).
I compared this to other functions on websites, but it really seems to be the same, and so I don't know what to do. ( The 6 spaces are missing in this post, but I put them before the statements)
I need a little help.

Thank you.

My program:

program alpha

real*8 b

b=1.
write(*,*) b
write(*,*) precision(b)

end

c**********


real*8 function precision(a)
real*8 c, a

write(*,*) a

c = 2.E-13

do while (a .LT. 100)
a=a*10.
c= c/10.
write(*,*) a, c
end do

precision = c

RETURN
END
 
Technology news on Phys.org
In your main program (alpha) you need to add

real*8 precision

Otherwise, the compiler thinks "precision" will return a real*4 variable (since its name starts with the letter p).

Without the real*8 declaration, the program might crash before it produced any output, or it might appear to run but give the wrong answers - but it certainly won't work correctly.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 37 ·
2
Replies
37
Views
5K