1

Please tell me where am i wrong, i couldn't fine my mistake in 2 programs. I try to use recursive in pascal.

This one is running but it gives me wrong resuts

program fatorial;
var
  n: integer;

function f(n: longint): longint;
begin
  if((n=0) or (n=1)) then 
    f:=1 
  else 
    *f:= n*f(n-1);*
  read(f);
end;

begin
  write('n:='); read(n);
  f(n);
  write('result:', f(n));
  readln;
end.

This one told me "Error: illegal expression" but i don't know how to fix it

program Greatest_common_divisor;
var
  gcd,p,q: integer;
  r:=real;
begin
  write('p:'); read(p);
  write('q:'); read(q);
  r:= p mod q;
  if r <> o then
  begin
    p:=q;
    q:=r
    *gcv:= gcv(q,r);*
  end;
  write('Greatest common divisor:', gcv(p.q));
  readln;
end.
Abstract type
  • 1,901
  • 2
  • 15
  • 26
HKS
  • 167
  • 1
  • 2
  • 10
  • 1
    Your first program gives correct results if you get rid of the `read(f)`. Why is that there? And you have to remove the asterisks from the outside of `*f:= n*f(n-1);*`. I'm not sure why you have them there. And why do you have the extra `f(n);` call on a line by itself? It serves no purpose. The result isn't stored. For your second function, why do you have asterisks around `gcv := gcv(q,r)`? Where is `gcv` defined? and `gcv(p.q)` is invalid since you have a period, not a comma. – lurker Sep 27 '15 at 12:20
  • 1
    The asterisks were probably emphasis (to show the recursion) from the wiki or forum page he copied this off :-) – Marco van de Voort Sep 27 '15 at 13:41
  • @MarcovandeVoort I figured the asterisks were to highlight something. I cited a few other issues which the OP needs to address. I figured a basic awareness on their part of what they're dumping into their question as source would be a desirable goal as well. :) – lurker Sep 28 '15 at 16:42

2 Answers2

2
  1. You should not read f in the function.

  2. You should write a function rather than use the internal function gcv()

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
TJM
  • 709
  • 1
  • 6
  • 11
0

First question:

  1. I think reading f in the function isn't correct.

But the second question:

  1. Don't use := in the command: r:=real; , only :
  2. o and gcv are what kind of variables? You didn't identify o and gcv after var .
  3. Put ; after q:=r
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Arshia
  • 27
  • 6