Difference of squares: function prototype imposed but not given

Hi! :wave:

I just did the “Difference of squares” exercise.
The only given prototype is difference. So, I created two functions squareOfSum and sumOfSquares.
Then, I got a strange error about my function name while compiling… until I realised the functions are called from the tests as square_of_sum and sum_of_squares.

I think it should be made clearer that these functions are needed (and how they should be called) without having to look into the test file.

I would be happy to open a PR! :wink:

1 Like

The exercise text is the same over all tracks and it purposefully avoids dropping any names.
Naming schemes vary widely over the 60+ languages on exercism, so it is more practical to leave that detail to the implementations on the individual tracks.

The track maintainers can add more information for each implemented exercise and it is generally used to point out particularities like special libraries.

Looking into the test files is strongly advised and aligns with Test Driven Development principles. That means the implementation is based on the specification that was used for the tests.

I understand your frustration from looking into several files for the correct path to the solution, but the method is actually quite close to real-life development situations.

2 Likes

Okay, if it’s a track design choice I can live with it. :smile:

I wasn’t thinking about modifying the instructions. A simple comment ! Function square_of_sum would do the trick.

I value TDD but I like to think of edge cases and how to divide the code myself. TDD with given tests removes the first step of adding a test, and therefore these reflections.
I guess I will do it before opening the test file (or I just won’t :see_no_evil: ).

Thanks for the reply! :wink:

I always like stubs that make things like this clearer. if you’d like to edit the stub to include something like this, that’d be great.

1 Like

Hmm, looking at that - does it not already have what you’re suggesting?

The clearer way would be to define the prototypes for square_of_sum and sum_of_squares, which are needed by the tests, the same way difference is provided.

It’s what I propose in this PR: Add missing function prototypes by Adrien-LUDWIG · Pull Request #216 · exercism/fortran · GitHub

If the goal is to let the student write the prototype, we could simply add comments with the name of the needed functions:

module difference_of_squares
  implicit none
contains

  ! Function square_of_sum

  ! Function sum_of_squares

  integer function difference(n)
    integer :: n

  end function difference

end module difference_of_squares

But it may not be as clear.

Tell me what you think is best. :wink:

What’s the difference in the output that the student gets on the first run? Does it make the initial error(s) clearer or harder to understand.

I like good stubs as a rule, but my priority is always clarity (as I think yours is too here).

Erik already merged the PR (yay, my first contribution :tada:). But for the sake of completeness, here is a comparison. :wink:

Running the tests on the previous stub gave a compilation error:

/tmp/difference-of-squares/difference_of_squares.f:::

     |   integer function difference(n)
      |                               
Warning: Unused dummy argument n at () [-Wunused-dummy-argument]
/tmp/difference-of-squares/difference_of_squares.f:::

     |   integer function difference(n)
      |                             
Warning: Return value of function difference at () not set [-Wreturn-type]
/tmp/difference-of-squares/difference_of_squares_test.f:::

     |    call assert_equal(, square_of_sum(), square of sum )
      |                        
Error: Function square_of_sum at () has no IMPLICIT type
/tmp/difference-of-squares/difference_of_squares_test.f:::

    |    call assert_equal(, square_of_sum(), square of sum )
      |                          
Error: Function square_of_sum at () has no IMPLICIT type
/tmp/difference-of-squares/difference_of_squares_test.f:::

    |    call assert_equal(, square_of_sum(), square of sum )
      |                               
Error: Function square_of_sum at () has no IMPLICIT type
/tmp/difference-of-squares/difference_of_squares_test.f:::

    |    call assert_equal(, sum_of_squares(), sum of squares )
      |                        
Error: Function sum_of_squares at () has no IMPLICIT type
/tmp/difference-of-squares/difference_of_squares_test.f:::

    |    call assert_equal(, sum_of_squares(), sum of squares )
      |                         
Error: Function sum_of_squares at () has no IMPLICIT type
/tmp/difference-of-squares/difference_of_squares_test.f:::

    |    call assert_equal(, sum_of_squares(), sum of squares )
      |                             
Error: Function sum_of_squares at () has no IMPLICIT type
make[]: *** [CMakeFiles/difference-of-squares.dir/build.make:: CMakeFiles/difference-of-squares.dir/difference_of_squares_test.f.o] Error 
make[]: *** [CMakeFiles/Makefile:: CMakeFiles/difference-of-squares.dir/all] Error 
make: *** [Makefile:: all] Error 

Now it compiles and each test can be explored to see what the expected output:

2 Likes

Great! Thank you (and thanks @ErikSchierboom :slight_smile:)

1 Like