Home > Software engineering >  The ' ' operation is not applicable to the types function(x: real): real and real. Check t
The ' ' operation is not applicable to the types function(x: real): real and real. Check t

Time:03-02

Check the operation of the program for a = 0.1; b = 1.0; h = 0.1; select the value of parameter n depending on the task.

enter image description here

enter image description here

Why am I getting an error? What is the best way to solve this problem? How to simplify?

var i, n: integer;
x, k, h, sx: real;

function Y(x: real): real;
begin
Y := x * arctan(x) - 0.5 * ln(1.0   x * x)
end;

function S(x: real): real;
var sum, xx, p, znak, e: real;
begin
S := 0.5 * x * x;
p := x * x;
xx := - x * x;
k := 2;
e := 1e303;
while abs(e) > 1e-14 do
begin
k := k   2;
p := p * xx;
e := p / (k * (k - 1));
S := S   e
end
end;

begin
h := 0.1;
writeln('x': 2, 'S(x)': 14,
'Y(x)': 18, 'n': 15);
for i := 1 to 10 do
begin
x := i * h;
sx := S(x);
n := round(k / 2);
writeln(x: 3: 1, sx: 18: 14,
Y(x): 18: 14, n: 10)
end
end.

-->The ' ' operation is not applicable to the types function(x: real): real and real

I tried to solve the problem based on the fact that x is the range a to b with a step h:

program test;
var y, a, b, h, x, Sx, Yx, n:real;
begin
  a:=0.1;
  b:=1.0;
  h:=0.1;
  
  x:=a;
  n:=0;
  
  while x<=b do
begin
  
  Yx:= x*arctan(x)-ln(sqrt(1 exp(x)));

  x:=x h;
  writeln(Yx);
  writeln('---------------------', n); n:=n 1;
  end;
 
end.

But I do not know how to get S(x)

CodePudding user response:

The error message means that the first argument of is a function. I'll bet this is the S := S e line. While you can assign to S to set the return value of S, you can't read it back like that.

You can refer to a function inside that function; this is used with recursion. But then you'll need to actually call yourself. E.g. Fibonacci := Fibonacci(i-1) * i. Now the left side of * is not a function, but the result of a function call.

Solution: just use a temporary variable, and assign that to S at the very end; of S

  • Related