1

So I'm just starting to learn Eiffel. One of the first exercises in the book I'm using says to make a function that does base^exp without using ^. I've copied my code below.

class
    APPLICATION

inherit
    ARGUMENTS

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        do
            create power(2;3)
            printf("2 to the power of 3 is " + answer)
        end

    power(base : REAL; exp : INTEGER) : REAL
        -- computers base raised to the bower of exp without using ^
        local
            remain : INTEGER
        do
            remain := exp
            if remain = 0 then
                result := 1
            else
                from
                until
                    remain = 0
                loop
                    result := result * result
                    remain := remain -1
                end
            end
        end
end

How do I use this? Do I need it on the same level as feature{NONE}'s make? I know how I'm calling it is wrong, and I can't find anything in the chapter I just read, or online on how to pass parameters into it or how to use it's results.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52

1 Answers1

6

There are several issues with the original code:

  1. create is used to create an object, but you are not going to create anything, but to get a result of a computation of the function power by calling it. Therefore the keyword create is not needed.

  2. You are using an entity answer to report the result of evaluation on a screen. However it is not declared anywhere. I believe the proper place would be a local variable declaration section.

  3. The entity answer is not initialized to the result of the function power. This is usually done by an assignment instruction.

  4. Feature arguments are separated by a comma, not by a semicolon.

  5. From the original code it's unclear what is the type of the variable answer. Assuming it matches the type of the function power, before adding it to a string, it needs to be converted to a string. This is done by calling the feature out.

  6. The standard feature for printing a string to a console is print, not printf.

Combining the critical points above, we get

make
        -- Run application.
    local
        answer: REAL
    do
        answer := power(2, 3)
        print ("2 to the power of 3 is " + answer.out)
    end

After that the code can be compiled. Now less critical points:

  1. It is a good style to put features to a dedicated feature clauses, so I would add a line like feature -- Basic operations before the feature power.

  2. The implementation of the feature power has at least two problems. I'm not going to detail them here, but would give two hints instead:

    • by default numeric Result is initialized to 0, this needs to be taken into account for operations that use it without first assigning any other value

    • even though an argument base is passed to the function power it remains unused in the original version of the code

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35