when I use the INTEGER in declaration in this code, I have no error with gfortran, but when I use REAL in declaration, I have this error:
Error: Type mismatch in argument 'a' at (1); passed INTEGER(4) to REAL(4)
here is my code:
Program workout
IMPLICIT none
REAL :: resultat
resultat = moyen(100,50)
WRITE(*,*) resultat
CONTAINS
REAL FUNCTION moyen(a,b)
IMPLICIT none
REAL,INTENT(IN) :: a, b
moyen = (a - b)/2
END FUNCTION moyen
End Program workout
thanks for your time
The error message is quite explicit, you are passing an integer argument to a function which expects a real argument. You could change this line
resultat = moyen(100,50)
to
resultat = moyen(100.0,50.0)
In fact, as you can see, you are making the same mistake twice in one line, the compiler is just identifying the first occurrence.
Related
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.
I'm sure the solution to this is extremely basic, but I'm having a hard time figuring out how to use functions in Fortran. I have the following simple program:
PROGRAM main
IMPLICIT NONE
INTEGER :: a,b
a = 3
b = 5
PRINT *,funct(a,b)
END PROGRAM
FUNCTION funct(a,b)
IMPLICIT NONE
INTEGER :: funct
INTEGER :: a,b
funct = a + b
END FUNCTION
I've tried several variations of this, including assigning a data type before FUNCTION, assigning the result of funct to another variable in the main program and printing that variable, and moving the FUNCTION block above the PROGRAM block. None of these worked. With the current program I get an error on line 6 (the line with the PRINT statement):
Error: Return type mismatch of function 'funct' (UNKNOWN/INTEGER(4))
Error: Function 'funct' has no IMPLICIT type
From all of the guides I've tried, I seem to be doing it right; at least one of the variations, or a combination of some of them, should have worked. How do I need to change this code to use the function?
Simply putting the function in the file will not make it accessible to the main program.
Traditionally, you could simply declare a function as external and the compiler would simply expect to find a suitable declaration at compile-time.
Modern Fortran organizes code and data in "modules". For your purpose, however, it is simpler to "contain" the function within the scope of the main program as follows:
PROGRAM main
IMPLICIT NONE
INTEGER :: a,b
a = 3
b = 5
PRINT *,funct(a,b)
CONTAINS
FUNCTION funct(a,b)
IMPLICIT NONE
INTEGER :: funct
INTEGER :: a,b
funct = a + b
END FUNCTION funct
END PROGRAM main
A simpler solution can be the following code
PROGRAM main
IMPLICIT NONE
INTEGER :: a,b, funct
a = 3
b = 5
PRINT *,funct(a,b)
END PROGRAM
FUNCTION funct(a,b)
IMPLICIT NONE
INTEGER :: funct
INTEGER :: a,b
funct = a + b
END FUNCTION
where the only difference is in the third line, where I have declared funct as an integer. It compiles and it prints 8 as result.
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 very new to programming and I cannot debug my program. Whenever I run it it gives the same error:
return type mismatch of function f at (1)
My code is:
real function F(x)
implicit none
real:: x
F=exp(-x)-x
end function
program easycod
implicit none
real::xl,xu,xr,fu,test,xrold,fl,fr
integer ::i
do i=1,50
xr=xrold
xr=(xl+xu)/2.0
fr =F(xr)
fl =F(xl)
test=fl*fr
IF (test>0.0) then
xl=xr
fr=fl
else if (test<0.0) then
xu=xr
end if
if (test==0.0) exit
print*,xr
end do
end program
There are a few things wrong with your code.
First, you are getting a compiler error because you haven't declared your function F in your program:
program easycode
implicit none
real :: xl, xu, xr, fu, test, xrold, fl, fr
real :: F ! <----------------- Add this line
integer :: i
Then you are assigning xr twice, which makes the first one unnecessary. Finally, xold, xl and xr are not initialized and can therefore be given any value the compiler would like.
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.