I am trying to write a very simple function in Fortran (first-time user):
program Main
implicit none
integer function k(n)
integer, intent(in) :: n
k=n
end function k
end program Main
I get a bunch of errors:
integer function k(n)
1
Error: Syntax error in data declaration at (1)
integer, intent(in) :: n
1
Error: Unexpected data declaration statement at (1)
end function k
1
Error: Expecting END PROGRAM statement at (1)
k=n
1
Error: Symbol ‘k’ at (1) has no IMPLICIT type
k=n
1
Error: Symbol ‘n’ at (1) has no IMPLICIT type
What am I doing wrong? I'm using the last version of gfortran.
Any declared functions and subroutines local to the program block should be put after a contains statement, for example
program Main
implicit none
contains
integer function k(n)
integer, intent(in) :: n
k=n
end function k
end program Main
To give an example of a program using this you could have
program Main
implicit none
integer :: myLocalN
myLocalN = 2
print*, "My local N is ", myLocalN
print*, "The value of this squared is", sq(myLocalN)
contains
integer function sq(n)
integer, intent(in) :: n
sq=n*n
end function sq
end program Main
Related
I would like to create a parametrized derived type inside a procedure and operate on the type's member. The following code works and writes out 4 and 8, as expected.
module mod1
implicit none
type :: pdt(n)
integer, len :: n
integer :: a(n), b(n)
end type
end module mod1
program main
use mod1
implicit none
type(pdt(4)) :: aa
print *, aa%n
print *, func(4)
contains
integer function func(n) result(m)
integer :: n
type(pdt(n)) :: pdttest
m = 2*pdttest%n
end function
end program
The problem arises when I place the procedure func in a separate module, in which case pdttest%n is uninitialized. Compiling using gfortran 11.2.0 with the flag -Wall returns:
Warning: ‘pdttest.n’ is used uninitialized [-Wuninitialized]
What could be the cause of this?
module mod2
use mod1
implicit none
contains
integer function func(n) result(m)
integer :: n
type(pdt(n)) :: pdttest
m = 2*pdttest%n
end function
end module
I am trying to write a very simple function in Fortran (first-time user):
program Main
implicit none
integer function k(n)
integer, intent(in) :: n
k=n
end function k
end program Main
I get a bunch of errors:
integer function k(n)
1
Error: Syntax error in data declaration at (1)
integer, intent(in) :: n
1
Error: Unexpected data declaration statement at (1)
end function k
1
Error: Expecting END PROGRAM statement at (1)
k=n
1
Error: Symbol ‘k’ at (1) has no IMPLICIT type
k=n
1
Error: Symbol ‘n’ at (1) has no IMPLICIT type
What am I doing wrong? I'm using the last version of gfortran.
Any declared functions and subroutines local to the program block should be put after a contains statement, for example
program Main
implicit none
contains
integer function k(n)
integer, intent(in) :: n
k=n
end function k
end program Main
To give an example of a program using this you could have
program Main
implicit none
integer :: myLocalN
myLocalN = 2
print*, "My local N is ", myLocalN
print*, "The value of this squared is", sq(myLocalN)
contains
integer function sq(n)
integer, intent(in) :: n
sq=n*n
end function sq
end program Main
Why am I getting Error: Unclassifiable statement?
I use Fortran 95 and use compile Fortran online : http://rextester.com/l/fortran_online_compiler
program main
implicit none
real::p = 0
p=3**5 * exp(-3)/call fact(5)
print*,p
end program main
function fact(n)
implicit none
integer :: n
integer :: i
integer :: fact
if (n < 0)then
fact = 1
else
do i = 2, n, 1
fact = fact * i
end do
end if
end function fact
error:
p=3**5 * exp(-3)/call fact(5)
1 Error: Unclassifiable statement at (1)
You cannot call a function using the call statement, that is only for subroutines. You just use the function name
p=3**5 * exp(-3.0)/fact(5)
The argument to exp must be real (like -3.0).
You either have to put the function into a module (better) or make it internal (between contains and end program), or at least declare its type
integer :: fact
in the program that is calling it.
Here is my code:
!lab 4(a) solution by James Ney
program lab4_a
implicit none
integer :: n
real :: L,R
interface
function testFun (x)
real :: testFun
real, intent (in) :: x
end function testFun
end interface
print *, "lab 4(a) solution by James Ney"
print *, "Enter left and right ends of interval and number of subintervals"
read *, L,R,n
call MeshCalcs(testFun,L,R,n)
contains
subroutine MeshCalcs(F,a,b,n)
implicit none
integer, intent(in) :: n
real, intent(in) :: a,b
real :: del,fVal,xVal
integer :: 1t=0,gr=0,i
real ::F,sum=0,average
del=(b-a)/real(n)
do i=0,n
xVal=a+(i*del)
fVal=F(xVal)
sum=sum+fVal
end do
Average=sum/(n+1.0)
print "('Average is: ',f10.2)",average
do i=0,n
xVal=a+(i*del)
fVal=F(xVal)
if (fVal>average) then
gr=gr+1
else if(fVal<average) then
1t=1t+1
end if
end do
print "('number of function values greater than average =',i4)",gr
print "('number of function values less than average =',i4)",1t
end subroutine MeshCalcs
end Program Lab4_a
real function testFun(x)
real, intent (in) :: x
testFun=-(x-4.0)**2+9.0
end function testFun
and the errors I get when I try to compile with gfortran are:
lab4_2a.f90:27.20:
integer :: 1t=0,gr=0,i
1
Error: Invalid character in name at (1)
lab4_2a.f90:43.5:
1t=1t+1
1
Error: Non-numeric character in statement label at (1)
lab4_2a.f90:43.6:
1t=1t+1
1
Error: Invalid character in name at (1)
lab4_2a.f90:47.62:
print "('number of function values less than average =',i4)",1t
1
Error: Syntax error in PRINT statement at (1)
lab4_2a.f90:41.5:
gr=gr+1
1
Error: Symbol 'gr' at (1) has no IMPLICIT type
lab4_2a.f90:30.12:
do i=0,n
1
Error: Symbol 'i' at (1) has no IMPLICIT type
The first error message is quite clear (well, clear to those who already know this stuff). In this line
integer :: 1t=0,gr=0,i
the first variable declared has a name beginning with the digit 1. Fortran's rules require that all names begin with a letter or an underscore. I believe that this is common in other programming languages too. So the compiler barfs on 1t and the rest of the errors shown are probably direct consequences. Rename that variable and see what happens.
I am using Fortran 90 and the Intel compiler.
I am very confused using a function in a subroutine. My code is (I deleted everything unimportant):
program test
INTEGER :: seed=5
REAL :: nor_ran_number1, nor_ran_number2
CALL Box_Muller_transform(seed,nor_ran_number1,nor_ran_number2)
end program test
double precision function grnd(SEED)
grnd=5
return
end
SUBROUTINE Box_Muller_transform (seed,nor_ran_number1,nor_ran_number2)
implicit none
INTEGER, INTENT(in) :: seed
REAL, INTENT(out) :: nor_ran_number1, nor_ran_number2
nor_ran_number1 = grnd(seed)
nor_ran_number2 = grnd(seed)
end SUBROUTINE Box_Muller_transform
The compiler returns:
error #6404: This name does not have a type, and must have an explicit
type. [GRND]
nor_ran_number1 = grnd(seed)
------------------^
I found this and understand that the function "grad" is not visible inside "Box_Muller_transform". However then I would expect the following code to produce the same error:
program test
INTEGER ::a=5, b
call sub(a,b)
write(*,*) b
end program
SUBROUTINE sub(a,b)
INTEGER, INTENT(in) ::a
INTEGER, INTENT(out) ::b
b = fun(a)
end subroutine sub
function fun(a)
INTEGER :: fun
INTEGER :: a
fun = a*a
end function fun
But this is working.
I would be very happy if someone could point out the difference and explain the simplest way to solve this problem.
Functions must have their return value defined. Since you are using implicit none in your first example, the type of the return value of grnd must be defined explicitly:
SUBROUTINE Box_Muller_transform (seed,nor_ran_number1,nor_ran_number2)
implicit none
INTEGER, INTENT(in) :: seed
REAL, INTENT(out) :: nor_ran_number1, nor_ran_number2
double precision :: grnd
nor_ran_number1 = grnd(seed)
nor_ran_number2 = grnd(seed)
end SUBROUTINE Box_Muller_transform
In the second example, you have not specified implicit none in sub, therefore fun is assumed to be of (implicit) type real. The compiler seems to silently cast this to integer.