How to create function that will raise x to y (x^y=z)

This code is not working:
[code]program hw13;
uses crt;
var number,exponent:integer;
function PowerFn (number, exponent: integer): integer;
begin
PowerFn := Exp(exponent*Ln(number));
end;
begin
clrscr;
write('Enter number: ');
readln(number);
write('Enter exponent: ');
readln(exponent);
writeln(number,'^',exponent,' = ',PowerFn(number,exponent));
readln;
end.[/code]

It gives me this error: Incompatible types: got "S80REAL" expected "LONGINT"

Comments

  • That's because Exp is a [b]real[/b] function and not integer. So change
    [code]function PowerFn (number, exponent: integer): [color=Green]real[/color];[/code]or change [code]PowerFn := [color=Green]Trunc([/color] Exp(exponent*Ln(number)) [color=Green])[/color][/code]
  • Actually, ou can use ** to represent the operand of ^ instead of using functions like those in your example.

    [code]
    PROGRAM x;
    USES crt;
    BEGIN
    writeln(13**5); {show 13^5}
    readkey
    END.
    [/code]
  • The program does not run on
  • Sorry that I forget to add the math unit.

    Change the second line to
    UWSES crt, math;,
    so you could use x**y to show x to the power of y
  • May be, you forgot a unit.
    That unit is maths unit (if i don't forgot !).
  • May be, you forgot a unit.
    That unit is maths unit (if i don't forgot !).
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories