Reputation: 1
I would appreciate some help on this. The point of the program is to take a lower bound, an upper bound, and the number of steps, and then input them into the respective chosen equation to create a matrix that is the length of the steps, that contains the values for for the equation that was chosen. I'm getting an unclassifiable statement at:
array(i)=function1(x)
array(i)=function2(x)
array(i)=function3(x)
I feel like it has something to do with how I declared my function, but I cannot figure out a fix to it. Any help would be appreciated.
PROGRAM stuff1 IMPLICIT NONE !variables INTEGER::i,step REAL::lower,upper,function1,function2,function3,x,q,w,e CHARACTER(20)::option REAL,ALLOCATABLE::array(:) !formats 101 FORMAT(A) !single text element only 102 FORMAT() ! <description> !-------Variable Definitions-------! ! ! ! !----------------------------------! !x= .1(upper-lower) !<Begin Coding Here> WRITE(*,101)"Hello User, please select a function to evaluate:" WRITE(*,101) WRITE(*,101)"A) f(x)=x^2+2x+4" WRITE(*,101)"B) f(x)=|x+4|" WRITE(*,101)"C) f(x)=sin(x)+42" WRITE(*,101)"Enter A,B,or C" DO READ(*,101)option IF ((option.EQ."A") .OR. (option.EQ."a")) THEN ELSE IF((option.EQ.'B') .OR. (option.EQ.'b'))THEN ELSE IF((option.EQ.'c') .OR. (option.EQ.'c'))THEN ELSE WRITE(*,*)"Please enter A,B,or C" CYCLE END IF EXIT END DO WRITE(*,101)"please enter an lower bound:" READ(*,*)lower WRITE(*,101) WRITE(*,101)"please enter an upper bound:" READ(*,*)upper WRITE(*,101) WRITE(*,101)"please enter a step size" READ(*,*)step function1=((x**2)+(2*x)+4) function2=(abs(x+4)) function3=(sin(x)+42) ALLOCATE(array(step)) x=lower DO i=1,step IF ((option.EQ."A") .OR. (option.EQ."a")) THEN array(i)=function1(x) ELSE IF((option.EQ.'B') .OR. (option.EQ.'b'))THEN array(i)=function2(x) ELSE IF((option.EQ.'c') .OR. (option.EQ.'c'))THEN array(i)=function3(x) END IF x=x+(upper-lower)/step END DO DO i=1,step WRITE(*,'(4F6.2)')array(i) END DO END PROGRAM
Upvotes: 0
Views: 1050
Reputation: 78316
These lines
function1=((x**2)+(2*x)+4)
function2=(abs(x+4))
function3=(sin(x)+42)
appear to be three statement functions. This is an obsolescent feature which you should not use, instead you should define functions along the lines
real function one(x)
real, intent(in) :: x
one = x**2 + 2*x + 4
end function one
If you must program like it's 1979 then the correct form for a statement function would be
function1(x)=((x**2)+(2*x)+4)
You have omitted the dummy argument in the definitions, it's no surprise to me that the compiler gets angry and issues that error.
Upvotes: 4
Reputation: 18098
That's not how to define a function in Fortran!
After the END PROGRAM
, define your functions like:
real function function1(x)
real,intent(in) :: x
function1=((x**2)+(2*x)+4)
end function
real function function2(x)
real,intent(in) :: x
function2=(abs(x+4))
end function
real function function3(x)
real,intent(in) :: x
function3=(sin(x)+42)
end function
Or, even better, put them into a module!
Upvotes: 1