Invalid character in name `1t` - fortran

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.

Related

passing logical variables to Fortran subroutines [duplicate]

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

error #6404: This name does not have a type, and must have an explicit type - using function in subroutine

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.

Declaring variables in function (Fortran)

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

invalid character at name 1

Trying to learn Fortran for a project. In a very simple program I am getting invalid character error.
program foo
implicit none
integer :: n_samp
integer :: samp_len
integer :: x_len
integer :: y_len
n_samp=2
samp_len=2
y_len=11
x_len=2
real(8),dimension(n_samp,samp_len,y_len,x_len)=Yvec
end program foo
error generated by GFORTRAN
t.f90:11.12:
real(8), dimension(n_samp,samp_len,y_len,x_len)=Yvec
1
Error: Invalid character in name at (1)
What is the cause of this error?
The correct syntax is
real(8), dimension(n_samp,samp_len,y_len,x_len) :: Yvec
The :: is obligatory when specifying any attributes (as the dimension in your case).
As #AlexanderVoigt points out, all variable declaration must be placed in the declaration part of the code, i.e., at the beginning.
I do not recommend using real(8) because that is not well defined, the 8 can mean anything, it is an index to a table of kinds and different compilers can have something different at place 8 in that table. See Fortran 90 kind parameter
That's simple: You are not allowed to have declarations in the main body (that is after some instructions)! Instead, you should use parameters:
program foo
implicit none
integer,parameter :: n_samp=2
integer,parameter :: samp_len=2
integer,parameter :: x_len=11
integer,parameter :: y_len=2
real(8),dimension(n_samp,samp_len,y_len,x_len) :: Yvec ! Add. typo here
end program foo

Fortran Function error about conflicting with a name in the encompassing scoping unit

I'm new in Fortran. what's the problem with this simple code?
program combinatorial
Implicit none
integer :: m, n, Fact
integer :: Com
Write (*,*) 'inter 2 number for m and n'
Read (*,*) m,n
Com = Fact (m)/(Fact(n)*Fact(m-n))
Contains
integer Function Fact(t)
Implicit none
Integer, intent(IN) :: t
integer :: i, Ans
Ans = 1
Do i=1, t
Ans=Ans * i
End do
Fact = Ans
End Function Fact
End program combinatorial
The error that i encounter is :
combinatorial.f90(10): error #6626: The name of the internal procedure conflicts with a name in the encompassing scoping unit. [FACT]
integer Function Fact(t)
-------------------------^
compilation aborted for combinatorial.f90 (code 1)
Since Fact is contained within the program the compiler will generate an interface to it automatically. By also declaring an integer thing called Fact you're giving the compiler conflicting instructions and it don't like that. Just drop Fact from the line
integer :: m, n, Fact
The encompassing scoping unit referred to by the compiler is the program which contains (or encompasses) the function.
And, as an aside, you don't need to use the variable Ans in your definition of the function. You could simply write
integer Function Fact(t)
Implicit none
Integer, intent(IN) :: t
integer :: i
Fact = 1
Do i=1, t
Fact = Fact * i
End do
End Function Fact
Unless you use a result clause on the function statement the compiler will behave as if it creates a variable of the same name as the function for returning the function's result.